【问题标题】:PHP Based HTML Validator基于 PHP 的 HTML 验证器
【发布时间】:2009-08-28 21:08:38
【问题描述】:

我需要找到一个基于 PHP 的 HTML(如 WC3-Like)验证器,它可以查找无效的 HTML 或 XHTML。我在 Google 上搜索了一下,但很好奇是否有人用过他们特别喜欢的。

我有一个字符串中的 HTML:

$html = "<html><head>.....</body></html>";

我希望能够测试页面,并让它返回错误。 (不回显/打印任何东西)

我见过:
-http://www.bermi.org/xhtml_validator
-http://twineproject.sourceforge.net/doc/phphtml.html

这样做的背景是我想要一个在每个页面上运行的函数/类,检查文件自上次访问日期(或类似日期)以来是否已被修改,如果它没有't,运行验证器,以便在编码时立即通知我无效的 HTML。

【问题讨论】:

    标签: php html validation


    【解决方案1】:

    没有必要在这个上重新发明轮子。已经有一个PEAR library that interfaces with the W3C HTML Validator API。他们愿意为你做这项工作,为什么不让他们呢? :)

    【讨论】:

    • 很酷,但你必须依赖他们的网络服务。这意味着您必须连接到公共互联网。不过非常整洁。
    • 这绝对是一个选择。
    【解决方案2】:

    虽然它不是严格意义上的 PHP,(它是一个可执行文件)我真正喜欢的是 w3c 的 HTML 整洁。它会显示 HTML 有什么问题,如果你愿意,可以修复它。它还美化了 HTML,因此它看起来不会一团糟。从命令行运行,易于集成到 php 中。

    检查一下。 http://www.w3.org/People/Raggett/tidy/

    【讨论】:

      【解决方案3】:

      如果你不能使用 Tidy(有时托管服务没有激活这个 php 模块),你可以使用这个 PHP 类:http://www.barattalo.it/html-fixer/

      【讨论】:

        【解决方案4】:

        我有一个案例,我需要检查部分 html 代码中是否存在不匹配和格式错误的标签(主要是,例如 ,这是我的示例中的常见错误),并且各种重型验证器无法使用。所以我最终在 PHP 中制作了自己的自定义验证例程,它粘贴在下面(如果您有不同语言的文本,您可能需要使用 mb_substr 而不是基于索引的字符检索)(注意它不解析CDATA 或脚本/样式标签,但可以轻松扩展):

        function check_html( $html )
        {
            $stack = array();
            $autoclosed = array('br', 'hr', 'input', 'embed', 'img', 'meta', 'link', 'param', 'source', 'track', 'area', 'base', 'col', 'wbr');
            $l = strlen($html); $i = 0;
            $incomment = false; $intag = false; $instring = false;
            $closetag = false; $tag = '';
            while($i<$l)
            {
                while($i<$l && preg_match('#\\s#', $c=$html[$i])) $i++;
                if ( $i >= $l ) break;
                if ( $incomment && ('-->' === substr($html, $i, 3)) )
                {
                        // close comment
                        $incomment = false;
                        $i += 3;
                        continue;
                }
                $c = $html[$i++];
                if ( '<' === $c )
                {
                    if ( $incomment ) continue;
                    if ( $intag )  return false;
                    if ( '!--' === substr($html, $i, 3) )
                    {
                        // open comment
                        $incomment = true;
                        $i += 3;
                        continue;
                    }
        
                    // open tag
                    $intag = true;
                    if ( '/' === $html[$i] )
                    {
                        $i++;
                        $closetag = true;
                    }
                    else
                    {
                        $closetag = false;
                    }
                    $tag = '';
                    while($i<$l && preg_match('#[a-z0-9\\-]#i', $c=$html[$i]) )
                    {
                        $tag .= $c;
                        $i++;
                    }
                    if ( !strlen($tag) ) return false;
                    $tag = strtolower($tag);
                    if ( $i<$l && !preg_match('#[\\s/>]#', $html[$i]) ) return false;
                    if ( $i<$l && $closetag && preg_match('#^\\s*/>#sim', substr($html, $i)) ) return false;
                    if ( $closetag )
                    {
                        if ( in_array($tag, $autoclosed) || (array_pop($stack) !== $tag) )
                            return false;
                    }
                    else if ( !in_array($tag, $autoclosed) )
                    {
                        $stack[] = $tag;
                    }
                }
                else if ( '>' ===$c )
                {
                    if ( $incomment ) continue;
                    
                    // close tag
                    if ( !$intag ) return false;
                    $intag = false;
                }
            }
            return !$incomment && !$intag && empty($stack);
        }
        

        【讨论】:

        • 编写自己的 HTML 解析器是一个非常糟糕的主意,尤其是当您的代码将用于不受信任的输入时。 HTML 非常复杂。 HTML5 的解析规则非常复杂,可以处理许多微妙的边缘情况。有关 HTML 的一些常见误解可能会导致“滚动您自己的”解析器,请参阅:alanhogan.com/html-myths#close-tags
        • 在某些情况下(例如我的情况),只需要一个非常简单的自定义解析器,而在其他地方找不到这么简单的解析器。所以这是为这种情况提供的,否则我完全同意你的看法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 2016-01-29
        • 1970-01-01
        • 2011-10-16
        • 1970-01-01
        • 2012-03-19
        • 2013-05-14
        相关资源
        最近更新 更多