title: How can I remove empty paragraphs from an HTML file using simple_html_dom.php?
body: <p>I want to remove empty paragraphs from an HTML document using <a href="http://simplehtmldom.sourceforge.net/manual.htm" rel="nofollow noreferrer">simple_html_dom.php</a>. I know how to do it using the DOMDocument class, but, because the HTML files I work with are prepared in MS Word, the DOMDocument's loadHTMLFile() function gives this exception "Namespaces are not defined".</p>
  
  <p>This is the code I use with the DOMDocument object for HTML files not prepared in MS Word:</p>
  
  <pre><code>&lt;?php
  /* Using the DOMDocument class */
  
  /* Create a new DOMDocument object. */
  $html = new DOMDocument("1.0", "UTF-8");
  
  /* Load HTML code from an HTML file into the DOMDocument. */
  $html-&gt;loadHTMLFile("HTML File With Empty Paragraphs.html");
  
  /* Assign all the &lt;p&gt; elements into the $pars DOMNodeList object. */
  $pars = $html-&gt;getElementsByTagName("p");
  
  echo "The initial number of paragraphs is " . $pars-&gt;length . ".&lt;br /&gt;";
  
  /* The trim() function is used to remove leading and trailing spaces as well as
  * newline characters. */
  for ($i = 0; $i &lt; $pars-&gt;length; $i++){
      if (trim($pars-&gt;item($i)-&gt;textContent) == ""){
          $pars-&gt;item($i)-&gt;parentNode-&gt;removeChild($pars-&gt;item($i));
          $i--;
      }
  }
  
  echo "The final number of paragraphs is " . $pars-&gt;length . ".&lt;br /&gt;";
  
  // Write the HTML code back into an HTML file.
  $html-&gt;saveHTMLFile("HTML File WithOut Empty Paragraphs.html");
  ?&gt;
  </code></pre>
  
  <p>This is the code I use with the simple_html_dom.php module for HTML files prepared in MS Word:</p>
  
  <pre><code>&lt;?php
  /* Using simple_html_dom.php */
  
  include("simple_html_dom.php");
  
  $html = file_get_html("HTML File With Empty Paragraphs.html");
  
  $pars = $html-&gt;find("p");
  
  for ($i = 0; $i &lt; count($pars); $i++) {
      if (trim($pars[$i]-&gt;plaintext) == "") {
          unset($pars[$i]);
          $i--;
      }
  }
  
  $html-&gt;save("HTML File without Empty Paragraphs.html");
  ?&gt;
  </code></pre>
  
  <p>It is almost the same, except that that the $pars variable is a DOMNodeList when using DOMDocument and an array when using simple_html_dom.php. But this code does not work. First it runs for two minutes and then reports these errors: "Undefined offset: 1"  and "Trying to get property of nonobject" for this line:  "if (trim($pars[$i]->plaintext) == "") {".</p>
  
  <p>Does anyone know how I can fix this?</p>
  
  <p>Thank you.</p>
  
  <p>I also asked on <a href="http://forums.devnetwork.net/viewtopic.php?f=1&amp;t=121337" rel="nofollow noreferrer">php devnetwork</a>.</p>