I have this post earlier regarding removing the html tags that have empty text node.
$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();
it works perfectly but I don’t want it to remove <br/> tag – how can I keep it?
Use this XPath (it excludes
brnodes):