【发布时间】:2011-10-12 01:59:34
【问题描述】:
我之前有这个post,关于删除具有空文本节点的html标签。
$dom = new DOMDocument();
$dom->loadHtml(
'<p><strong><a href="http://xx.org.uk/dartmoor-arts">test</a></strong></p>
<p><strong><a href="http://xx.org.uk/depw"></a></strong></p>
<p><strong><a href="http://xx.org.uk/devon-guild-of-craftsmen"></a></strong></p>
<p>this line has a <br/>break</p>
'
);
$xpath = new DOMXPath($dom);
while(($nodeList = $xpath->query('//*[not(text()) and not(node())]')) && $nodeList->length > 0) {
foreach ($nodeList as $node) {
$node->parentNode->removeChild($node);
}
}
echo $dom->saveHtml();
它运行良好,但我不希望它删除 <br/> 标签 - 我怎样才能保留它?
【问题讨论】: