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.

<?php
/**
 * W3cValidate
 * 
 * For more information on this file and how to use the class please visit
 * http://www.hashbangcode.com/blog/w3c-validation-php-class-1300.html
 *
 * @author       Philip Norton
 * @version   1.0
 * @copyright 2009 #! code
 * 
 */
 
/**
 * This class allows the retreival of W3C HTML validation results.
 *
 * @package    W3cValidate
 */
class W3cValidate{
    
    /**
     * The URL used in the test
     *
     * @var string
     */
    private $url;
 
    /**
     * The W3C validation results for tested URL
     *
     * @var integer
     */
    public $result;
    
    /**
     * Constructor 
     *
     * @param string $url The URL that will be used in the test.
     */
    public function W3cValidate($url){
        // Make sure the URL has http in front of it
        $url = str_replace("http://","",$url);
        $url = "http://".$url;
        $this->url = $url;
    }
    
    /**
     * Get the results from the W3C validation site and use regular expressions to return a result. 
     *
     * @return integer The number of errors, or -1 if an error was encountered.
     */
    public function getValidation(){    
        $w3cvalidator = strip_tags(file_get_contents("http://validator.w3.org/check?uri=".$this->url));
        // Validator response is null
        if ( $w3cvalidator == '' ) {
            $this->result = -1;
            return $this->result;
        }
        // Validator responded, check results
        preg_match_all('/(?<=Result:)\s+(\d)*(?= errors?)/i', $w3cvalidator, $matches);
        if ( isset($matches[0][0]) ) {
            $this->result = trim($matches[0][0]);
            return $this->result;
        }
        // Check for broken document
        preg_match_all('/Sorry! This document can not be checked\./i', $w3cvalidator, $matches);
        if ( isset($matches[0][0]) ) {
            $this->result = -1;
            return $this->result;                    
        }
        // Document is valid
        $this->result = 0;
        return $this->result;
    }
}

You can use the class like this:

$w3c = new W3cValidate("http://www.hashbangcode.com");
echo $w3c->getValidation();
 
$w3c = new W3cValidate("http://www.amazon.co.uk");
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 application.

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.

Comments

Thanks for the good information...very good blog site.
Permalink

One of the basic reasons why websites often do not pass the W3C validation check is due to the fact that there are several programs and scripts that are running on the web pages and many of them do not comply with the web standards.

Permalink

The class works well, provided you make one tiny change. Currently the regular expression only matches 2 or more errors because it looks for the "s" at the end of errors. You could make this change to handle detecting a single error.

 

preg_match_all('/(?<=Result:)\s+(\d)*(?= errors)/i', $w3cvalidator, $matches); 

 

 

preg_match_all('/(?<=Result:)\s+(\d)*(?= errors?)/i', $w3cvalidator, $matches);

 

The question mark just makes the 's' optional. Works perfectly after that. 

 

 

Permalink

Corrected. Thanks for the input! :)

Name
Philip Norton
Permalink

Gracias..

Permalink
Dear Philip, Now they support JSON only. So please update your class :) Thanks! Sandeep
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.