Manipulating DOM in PHP
<p>Although I've been using <a href="http://symfony.com/doc/current/components/dom_crawler.html" rel="nofollow noreferrer">DomCralwer</a> throughout my project, but it has a bug with this XPath query <code>//table[*[self::tbody or self::thead or self::tfoot]/tr[count(*) = 2]]</code> therefore, as a workaround, I'm trying to use the built-in functions:</p>

<pre><code>for ($i=0; $i &lt; count($tables); $i++) {

  $tables[$i] = purifyTables($tables[$i]);
  echo($tables[$i]);

  $dom = new DOMDocument();
  $dom-&gt;loadHTML( $tables[$i] );
  $xpath = new DOMXPath($dom);
  $nodes =  $xpath-&gt;query("//table[*[self::tbody or self::thead or self::tfoot]/tr[count(*) = 2]]");
  echo($nodes);
  echo($dom-&gt;saveHTML($nodes));

}
</code></pre>

<p>... but the output is unexpected:</p>

<pre><code>&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;
      WII 5-IN-1 SPORTS KIT H7/10 5-in-1 Glow-in-the-Dark Sports
      Pack for Wii
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;
      Our Price: $19.99 Sale: $5.00 You Save: 75%
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
DOMNodeList Object
(
    [length] =&gt; 0
)

PHP Warning:  DOMDocument::saveHTML() expects parameter 1 to be DOMNode, object given in /var/www/html/myscript.php on line 195

Warning: DOMDocument::saveHTML() expects parameter 1 to be DOMNode, object given in /var/www/html/myscript.php on line 195
</code></pre>