HTML 使用 PHP 进行验证

HTML Validation using PHP

我在一台服务器上有许多网站,每天都有多名员工在处理这些网站。由于有这么多人处理这些文件,可能很难跟上某些事情的步伐。我正在构建一个基于 PHP 的工具,该工具将扫描这些页面以查找损坏的链接、拼写错误和其他类似内容。

现在我正在处理此的 HTML 验证部分,即 missing/extra 标记关闭和打开。我在这里 (PHP Based HTML Validator) 上找到了一个 post,它引导我找到一个与 W3C 验证链接的 pear 包。

我还没有尝试过,因为发布的最后一个版本已经快 5 年了。谁能帮我把鼻子指向正确的方向。

我有一个验证应用程序 运行s 多重验证。

首先我查看页面的基本参数和服务器配置。

在范围内的参数,打印输出绿色,超出范围:红色,介于两者之间:黄色

Web Server Configuration
Base Page HTML
Compression enabled: Yes
Cache specified: Yes max-age=31536000, public
Expiration specified: Yes
Keep Alive Specified: Yes
Characterset Specified: Yes
Web Server Performance Metrics
Base Page HTML
Base Page Size: 17,126 Bytes
Transmission Speed: 2,137,041 Bytes/Sec.
Compression: 1.9X
HTML Whitespace: 0.0%
Bytes Transmitted: 9,029 Bytes
HTML Transfer Rate: 4,053,491 Bytes/Sec.
Resolve Domain Name: 0.107 Sec.
Connect Time: 0.107
Transfer Time: 0.004 Sec.
Generate HTML: 0.195 Sec.
Total Time: 0.307 Sec.

然后我运行这4个带有curl的验证工具。

  • W3C HTML 标记
  • W3C CSS 验证服务
  • W3C mobileOK 检查器
  • 网页测试

然后报告如下结果:

  • 无CSS 错误
  • 无HTML 错误
  • W3C mobileOK 96%
  • 页面速度得分:99%

对于这项工作 PHP 内置函数 array libxml_get_errors ( void ) 将 return 错误数组。看看this documentation。还有一个例子。

我对页面正文的测试:

<?php

libxml_use_internal_errors(true);

$xmlstr = <<< XML
    <body>
        <h1>Correct tag</h1>
        <h2>Tag not closed</h2>
        <p>Missing end of paragraph
        <br>
        <script type="text/javascript">
        var test = "Script";
        </script>
        <img src="some.url" alt="Image title" >
        <footer>Some error in footer?<footer>
    </body>
XML;

$doc = simplexml_load_string($xmlstr);
$xml = explode("\n", $xmlstr);

if (!$doc) {
    $errors = array_reverse ( libxml_get_errors() );
    echo "<pre>";
    foreach ($errors as $error) {
        echo display_xml_error($error, $xml);
    }
    echo "</pre>";
    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";
}

?>

结果:

---------^
Fatal Error 77: Premature end of data in tag body line 1

--------------------------------------------


---------^
Fatal Error 77: Premature end of data in tag p line 4

--------------------------------------------


---------^
Fatal Error 77: Premature end of data in tag br line 5

--------------------------------------------


---------^
Fatal Error 77: Premature end of data in tag img line 9

--------------------------------------------


---------^
Fatal Error 77: Premature end of data in tag footer line 10

--------------------------------------------


---------^
Fatal Error 76: Opening and ending tag mismatch: footer line 10 and body

--------------------------------------------

不要与 body 未关闭的错误相混淆。如果 HTML 有效,则不会丢失任何错误。比如下面的代码根据数组libxml_get_errors()没有错误:

<body>
    <h1>Correct tag</h1>
    <h2>Tag closed</h2>
    <p>Not missing end of paragraph</p>
<br />
    <script type="text/javascript">
    var test = "Script";
    </script>
        <img src="some.url" alt="Image title" />
        <div class="somediv">
            <p>Paragraph nested</p> 
            <ul>
                <li>List element</li>
                <li>List element</li>
            </ul>
        </div>
        <footer>No error in footer</footer>
</body>