I am using http://de2.php.net/manual/en/domdocument.validate.php to validate an HTML file. Below is the code. But every time I validate an HTML file, it says it's not valid. Is it an error with the code or is there any better PHP validators?
<?php
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->load('book.html'); // see docs for load, loadXml, loadHtml and loadHtmlFile
if ($dom->validate()) {
echo "This document is valid!
";
}
else
echo "Its not valid";
?>
Ok guys. Got that myself. Here is the code...
libxml_use_internal_errors(true);
$doc = simplexml_load_string($html);
$xml = explode("
", $html);
$flag=0;
if (!$doc) {
$errors = array_reverse ( libxml_get_errors() );
foreach ($errors as $error) {
$flag++;
}
libxml_clear_errors();
}
function display_xml_error($error, $xml)
{
$return = $xml[$error->line - 1] . "
";
$return .= str_repeat('-', $error->column) . "^
";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "Warning $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return .= "Error $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return .= "Fatal Error $error->code: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= "
File: $error->file";
}
return "$return
--------------------------------------------
";
}
if ($flag==0)
echo "This document is valid.<br/>";
else
echo "Its not valid html. Contains ".$flag." errors.<br/>";