我一直在用 DOM 处理这个 tidy-up-messy-html 标签,但现在我意识到一个更大的问题,
$content = '<p><a href="#">this is a link</a></p>';
function tidy_html($content,$allowable_tags = null, $span_regex = null)
{
$dom = new DOMDocument();
$dom->loadHTML($content);
// other codes
return $dom->saveHTML();
}
echo tidy_html($content);
它将输出整个 DOM,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p><a href="#">this is a link</a></p></body></html>
但我只想要这样的回报,
<p><a href="#">this is a link</a></p>
我不想,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>...</body></html>
这可能吗??
EDIT:
the innerHTML
模拟在我的数据库中生成一些奇怪的代码,例如
, Â
, ’
<p>Monday July 5th 10am - 3.30pm £20</p>
<p>Be one of the first visitors to the ...at this special event.Â</p>
<p>All participants will receive a free copy of the ‘Contemporary Art Kit’ produced exclusively for Art on....</p>
the innerHTML
模拟,
$innerHHTML = '';
$nodeBody = $dom->getElementsByTagName('body')->item(0);
foreach($nodeBody->childNodes as $child) {
$innerHTML .= $nodeBody->ownerDocument->saveXML($child);
}
我发现它在中断时创建奇怪代码的原因是saveXML($child)
所以当我有这样的事情时
$content = '<p><br/><a href="#">xx</a></p>
<p><br/><a href="#">xx</a></p>';
它会返回这样的东西,
<p><a href="#">xx</a></p>
<p><a href="#">xx</a></p>
但我实际上想要这样的东西,
<p><a href="#">xx</a></p>
<p><a href="#">xx</a></p>