Reputation: 57176
How can I remove this type p tag <p> </p>
using DOM or regex?
I want to remove multiple p like this too,
<p> </p>
<p> </p>
<p> </p>
Upvotes: 0
Views: 4942
Reputation: 197659
In case you would like to do that with xpath (your example is just demanding str_replace
however), you can query the  
entity as a string (Demo):
$html = '<body><p> </p>
<p> </p>
<p> </p>
<p>Not empty :)</p>
</body>';
$dom = new DomDocument();
$dom->loadhtml($html);
$xpath = new DomXPath($dom);
$col = $xpath->query("//p[text()=\"\xC2\xA0\"]"); #
foreach($col as $e) {
$e->parentNode->removeChild($e);
}
echo $dom->saveXML($dom->getElementsByTagName('body')->item(0));
Hope this is helpful if you need to query
with xpath.
See as well: Using XPATH to search text containing
Upvotes: 0
Reputation: 400942
If you want to remove a string that is exactly, always, '<p> </p>'
, the simplest and fastest solution is probably to use str_replace()
:
$new_string = str_replace('<p> </p>', '', $old_string);
I don't think it's necessary to use DOM for such a simple case -- and a regex is not necessary here.
Of course, if you need to replace something more complex, that is not always exactly the same string... well, it'll be time for DOM manipulations ;-)
Upvotes: 5
Reputation: 50966
preg_replace("|<p> </p>|", "", "<p> </p>
<p> </p>
<p> </p>");
Upvotes: 0