Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP内置Dom方法验证HTML_Php_Html_Validation - Fatal编程技术网

使用PHP内置Dom方法验证HTML

使用PHP内置Dom方法验证HTML,php,html,validation,Php,Html,Validation,我正在使用验证HTML文件。下面是代码。但每次我验证HTML文件时,它都会说它无效。这是代码错误还是有更好的PHP验证程序 <?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

我正在使用验证HTML文件。下面是代码。但每次我验证HTML文件时,它都会说它无效。这是代码错误还是有更好的PHP验证程序

<?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!\n";
}
else
    echo "Its not valid";
?>
load('book.html');//有关load、loadXml、loadHtml和loadHtmlFile,请参见文档
如果($dom->validate()){
echo“此文档有效!\n”;
}
其他的
附和“它无效”;
?>

好的,伙计们。我自己弄的。这是代码

libxml_use_internal_errors(true);

$doc = simplexml_load_string($html);
$xml = explode("\n", $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] . "\n";
    $return .= str_repeat('-', $error->column) . "^\n";

    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 .= "\n  File: $error->file";
    }

    return "$return\n\n--------------------------------------------\n\n";
}
if ($flag==0)
    echo "This document is valid.<br/>";
else
    echo "Its not valid html. Contains ".$flag." errors.<br/>";
libxml\u使用\u内部错误(true);
$doc=simplexml\u load\u字符串($html);
$xml=explode(“\n”,$html);
$flag=0;
如果(!$doc){
$errors=array_reverse(libxml_get_errors());
foreach($errors作为$error){
$flag++;
}
libxml_clear_errors();
}
函数显示\u xml\u错误($error,$xml)
{
$return=$xml[$error->line-1]。“\n”;
$return.=str_repeat('-',$error->column)。“^\n”;
开关($error->level){
案例LIBXML_ERR_警告:
$return.=“警告$error->code:”;
打破
案例LIBXML_ERR_ERROR:
$return.=“错误$Error->code:”;
打破
案例LIBXML_ERR_致命:
$return.=“致命错误$Error->code:”;
打破
}
$return.=trim($error->message);
如果($error->file){
$return.=“\n文件:$error->File”;
}
return“$return\n\n--------------------------------------------\n\n”;
}
如果($flag==0)
echo“本文件有效。
”; 其他的 echo“它的html无效。包含“$flag.”错误。
”;
我认为
validate
更适合于验证xml而不是html。您可以尝试
$dom->validateOnParse=true但是我认为你不会像现在的itOk那样对validate函数感到高兴。我尝试过搜索其他php html验证程序。但是找不到。也不能使用任何在线api