php - 使用 Tidy 在 PHP 中验证 HTML5 文档

标签 php html tidy

我正在尝试清理 HTML 字符串并使用 Tidy 和 PHP 创建 HTML5 文档,但是,我正在创建 HTML3.2 文档。如图所示,我收到一个 Config: missing or malformed argument for option: doctype 错误。我在 Centos 6 和 Apache 2.2 上运行 PHP 版本 5.5.35,php_info() 显示如下:

tidy

Tidy support    enabled
libTidy Release 14 June 2007
Extension Version   2.0 ($Id: e066a98a414c7f79f89f697c19c4336c61bc617b $)

Directive   Local Value Master Value
tidy.clean_output   no value    no value
tidy.default_config no value    no value

如何创建 HTML5 文档?以下是我的尝试:

<?php
$html = <<<EOD
<p>Hello</p>
<div>
 <p data-customattribute="will be an error">bla</p>
 <p>bla</p>
</div>
<div>
 <p>Hi there!</p>
 <div>
  <p>Opps, a mistake</px>
 </div>
</div>
EOD;
$html="<!DOCTYPE HTML><html><head><title></title></head><body>$html</body></html>";

echo($html."\n\n");

    $config = array(
        'indent' => true,
        'indent-spaces' => 4,
        'doctype' => '<!DOCTYPE HTML>',
    );

$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
print_r($tidy);

输出

<!DOCTYPE HTML><html><head><title></title></head><body><p>Hello</p>
<div>
 <p data-customattribute="will be an error">bla</p>
 <p>bla</p>
</div>
<div>
 <p>Hi there!</p>
 <div>
  <p>Opps, a mistake</px>
 </div>
</div></body></html>

tidy Object
(
    [errorBuffer] => Config: missing or malformed argument for option: doctype
line 9 column 21 - Warning: discarding unexpected </px>
line 3 column 2 - Warning: <p> proprietary attribute "data-customattribute"
    [value] => <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p>
            Hello
        </p>
        <div>
            <p data-customattribute="will be an error">
                bla
            </p>
            <p>
                bla
            </p>
        </div>
        <div>
            <p>
                Hi there!
            </p>
            <div>
                <p>
                    Opps, a mistake
                </p>
            </div>
        </div>
    </body>
</html>
)

最佳答案

Old versions of Tidy do not support HTML5 documents

第一个支持 HTML 5 的 tidy 发布于 Sep 2015 , 其中 HTML Tidy Advocacy Community Group发布了 tidy-html5 的第一个版本。

请注意,您使用的是旧版本的 tidy,因此您将无法验证 html5 文档。

当前预编译版本的 php 还没有用 tidy-html5 编译,所以如果你想使用 tidy-html5,你必须自己编译它。

这些说明摘自 README tidy-html5 github 中的文件:

Due to API changes in the PHP source, "buffio.h" needs to be changed to "tidybuffio.h" in the file ext/tidy/tidy.c.

That is - prior to configuring php run this in the php source directory:

   sed -i 's/buffio.h/tidybuffio.h/' ext/tidy/*.c

And then continue with (just an example here, use your own php config options):

   ./configure --with-tidy=/usr/local
   make
   make test
   make install

关于php - 使用 Tidy 在 PHP 中验证 HTML5 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422918/

相关文章:

Php 找不到 tidy

php - 通过可迭代注入(inject)的标记服务中的类名获取服务

php - Laravel 获取字符串上的 url

php - 上传图片时获取图片名称

html - CSS 媒体查询不覆盖

C++ 整洁链接器错误

php - 在卖家网站上 promise 付款

html - 将 Font Awesome 图标添加到 css 类

Javascript - EventListener 对两侧整个跨度的点击使用react

php - 清理 PHP/HTML 页面