使用 Tidy 验证 PHP 中的 HTML5 文档
Validate HTML5 Document in PHP using 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
第一个版本tidy
支持HTML5是在Sep 2015, where the HTML Tidy Advocacy Community Group发布第一个tidy-html5版本。
请注意,您使用的是旧版本的 tidy,因此您将无法验证 html5 个文档。
php 的当前预编译版本尚未使用 tidy-html5 进行编译,因此如果您想使用 tidy-html5,则必须自己进行编译。
这些说明取自 tidy-html5 github:
中的 README 文件
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
我正在尝试清理一个 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
第一个版本tidy
支持HTML5是在Sep 2015, where the HTML Tidy Advocacy Community Group发布第一个tidy-html5版本。
请注意,您使用的是旧版本的 tidy,因此您将无法验证 html5 个文档。
php 的当前预编译版本尚未使用 tidy-html5 进行编译,因此如果您想使用 tidy-html5,则必须自己进行编译。
这些说明取自 tidy-html5 github:
中的 README 文件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