title: DOMDocument in php
body: <p>I have just started reading documentation and examples about DOM, in order to crawl and parse the document.</p>
  
  <p>For example I have part of document shown below:</p>
  
  <pre><code>    &lt;div id="showContent"&gt;
      &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
           Crap
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;tr&gt;
            &lt;td width="172" valign="top"&gt;&lt;a href="link"&gt;&lt;img height="91" border="0" width="172" class="" src="img"&gt;&lt;/a&gt;&lt;/td&gt;
            &lt;td width="10"&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td valign="top"&gt;&lt;table cellspacing="0" cellpadding="0" border="0"&gt;
                &lt;tbody&gt;&lt;tr&gt;
                  &lt;td height="30"&gt;&lt;a class="px11" href="link"&gt;title&lt;/a&gt;&lt;a&gt;&lt;br&gt;
                      &lt;span class="px10"&gt;&lt;/span&gt;
                  &lt;/a&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;&lt;img height="1" width="580" src="crap"&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;
                      &lt;a href="link"&gt;&lt;img height="16" border="0" width="65" src="/buy"&gt;&lt;/a&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td valign="top" class="px10"&gt;
                      &lt;p style="width: 500px;"&gt;description.&lt;/p&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
  Crap
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
           Crap
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I'm trying to use the following code to get all the <code>tr</code> tags and analyze whether there is crap or information inside them:</p>
  
  <pre><code>$dom = new DOMDocument();
  @$dom-&gt;loadHTML($html);
  
  $xpath = new DOMXPath($dom);
  
  
  $tags = $xpath-&gt;query('.//div[@id="showContent"]');
  foreach ($tags as $tag) {
      $string="";
      $string=trim($tag-&gt;nodeValue);
      if(strlen($string)&gt;3) {
          echo $string;
          echo '&lt;br&gt;';
      }
  }
  </code></pre>
  
  <p>However I'm getting just stripped string without the tags, for example:</p>
  
  <pre><code>Crap
  
  Crap
  Title
  Description
  </code></pre>
  
  <p>But I would like to get:</p>
  
  <pre><code>&lt;tr&gt;
     &lt;td&gt;Crap&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
     &lt;a href="link"&gt;title&lt;/a&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>How to keep html nodes (tags)?</p>