Friday, 10 August 2018

W3C Validation PHP Class

If you want to incorporate a W3C validation check into an application then you can use the following class. It uses file_get_contents() to get the contents of the file and then uses regular expressions to return either the number of errors or -1 is any errors occur. Of course if the document is valid the function will return 0.
  1. url = $url;
  2. }
  3.  
  4. /**
  5.   * Get the results from the W3C validation site and use regular expressions to return a result.
  6.   *
  7.   * @return integer The number of errors, or -1 if an error was encountered.
  8.   */
  9. public function getValidation(){
  10. $w3cvalidator = strip_tags(file_get_contents("http://validator.w3.org/check?uri=".$this->url));
  11. // Validator response is null
  12. if ( $w3cvalidator == '' ) {
  13. $this->result = -1;
  14. return $this->result;
  15. }
  16. // Validator responded, check results
  17. preg_match_all('/(?result = trim($matches[0][0]);
  18. return $this->result;
  19. }
  20. // Check for broken document
  21. preg_match_all('/Sorry! This document can not be checked\./i', $w3cvalidator, $matches);
  22. if ( isset($matches[0][0]) ) {
  23. $this->result = -1;
  24. return $this->result;
  25. }
  26. // Document is valid
  27. $this->result = 0;
  28. return $this->result;
  29. }
  30. }
You can use the class like this:
  1. $w3c = new W3cValidate("http://www.hashbangcode.com");
  2. echo $w3c->getValidation();
  3.  
  4. $w3c = new W3cValidate("http://www.amazon.co.uk");
  5. echo $w3c->getValidation();
The first result for #! code returns 0 errors, whereas the second result for Amazon UK returns 1,565 errors. I used Amazon as an extreme example as I know that there are many HTML errors.
You can download the W3C validation code from the W3C site and install it on your own server, but this codebase is written in perl. This class is intended as a short cut to getting W3C validation results without having to integrate this into your PHP applicaiton.
There are also two points to remember about this class. First, there is a chance that the W3C will alter the output on their page and therefore change the returned results. This will essentially break the class so it is always useful to double check the results once in a while. Secondly, if you use this class to do too much you will find yourself being blocked from the W3C validation site due to overuse.

0 comments:

Post a Comment