title: Remove all empty html elements using PHP: DOMDocument
body: <p>Is there any way to remove all empty elements from an html without using regex?</p>
  
  <p>I did this with <code>DOMXPath</code></p>
  
  <pre><code>$this-&gt;dom-&gt;loadHTML($document, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  $xpath = new \DOMXPath($this-&gt;dom);
  while (($node_list = $xpath-&gt;query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) &amp;&amp; $node_list-&gt;length) {
      foreach ($node_list as $node) {
          $node-&gt;parentNode-&gt;removeChild($node);
      }
  }
  </code></pre>