Tidy is a binding for the Tidy HTML clean and repair utility which allows you to not only clean and otherwise manipulate HTML documents, but also traverse the document tree.有的时候,页面工程师给的HTML并不是很完美, 标签混乱, 结构不太容易看请, 这个时候Tidy也是可以大显身手的。
呵呵,测试如下:
<?php
ob_start();
echo <<<html
<html>
<head>
<title>test</title>
</head>
<body>
<p>error</i>
</body>
</html>
HTML;
$buffer = ob_get_clean();
$config = array('indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200);
$html = tidy_parse_string($buffer, $config, 'UTF8');
$html->cleanRepair();
$html = $html->value;
print_r($html);
?>输出 :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
test
</title>
</head>
<body>
<p>
error
</p>
</body>
</html>错误被修正,并且加入了缩进, 呵呵,是不是看起来,整洁了很多呢?
尤其当使用在线编辑器的时候, 更是应该把用户的输入,都tidy一下,修正错误。;)