PDA

Pogčedajte punu verziju : W3C Validator PHP Class


webarto
15. 02. 2012., 19:57
class W3C{

/**
* @author Webarto.com
* @copyright 2011
*/

public function check($url)
{

$html = $this->curl("http://validator.w3.org/check?uri=$url");
$html = strip_tags($html);
$html = str_replace(array("\t", "\n"), "", $html);
$html = preg_replace("#(\s){1,}#is", "\\1", $html);

$result["url"] = $url;
$result["result"] = $this->match("Result: (.*?) Address", $html);
$result["encoding"] = $this->match("Encoding: (.*?) \(detect automatically\)", $html);
$result["doctype"] = $this->match("Doctype: (.*?) \(detect automatically\)", $html);
$result["valid"] = (strpos($html, "[Valid]") !== false)? true: false;

return $result;

}

public function boolean($url)
{

$return = false;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://validator.w3.org/check?uri=$url");
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);

$status = $this->match("X-W3C-Validator-Status: ([\S]+)", $data);
if($status == "Valid"){
$return = true;
}else{
$return = false;
}
return $return;

}

private function curl($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}

private function match($regex, $string)
{
preg_match("#$regex#is", $string, $matches);
return $matches[1];
}

}


Full check.


include_once 'class.w3c.php';
$w3c = new W3C;
$result = $w3c->check("http://example.com");
$valid = ($result["valid"] == true) ? "Valid": "Invalid";
echo "Website {$result[url]} is W3C {$valid} ({$result[result]}), checked as {$result[doctype]} and with {$result[encoding]} encoding.";


Quick check.

include_once 'class.w3c.php';
$w3c = new W3C;

$uris = array(
"http://activeden.net/",
"http://audiojungle.net/",
"http://themeforest.net/",
"http://videohive.net/",
"http://graphicriver.net/",
"http://3docean.net/",
"http://codecanyon.net/",
"http://marketplace.tutsplus.com/"
);

foreach($urls as $url){
$x = $w3c->boolean($url);
echo ($x == true)? "{$url} is Valid!": "{$url} is Invalid!";
echo '<br />';
}