Using XPath expressions can save a lot of programming
and allow you to home in on only the nodes you want.
Suppose you want to delete all empty <p> tags.
If you create a query using the following XPath expression,
you can find <p> tags that do not have any text
(other than spaces), any attributes,
any children or comments:
$expression = "//p[not(@*)
and not(*)
and not(./comment())
and normalize-space(text())='']";
This expression will only find para tags that look like:
<p>[any number of spaces]</p>
<p></p>
Imagine the code you would have to add if you used
DOMDocument::getElementsByTagName("p") instead.