Search code examples
phpdomdocumentdomxpath

php domdocument or domxpath: how to extract TRs and save html


I have been struggling with this all day.

I have an html table in a string.

<TABLE>
  <TBODY>
    <TR CLASS=dna1>
      <TD></TD><TD></TD><TD></TD><TD></TD>
    </TR>
    <TR CLASS=dna2>
      <TD></TD><TD></TD><TD></TD><TD></TD>
    </TR>
    repeat...

Inside the <TD> are some <DIV> and <SPAN> that I need to work with.

I need to extract each <TR> (both classes) and save the html in an array where each <TR> is an array element.

Creating a node list array is easy enough, but how do I get the actual html?


Solution

  • If you must save the HTML as a string, there is DOMDocument::saveHTML

    $elems = $xpath->query('//tr');
    
    foreach ($elems as $elem) {
      $array[] = $doc->saveHTML($elem);
    }
    

    (Note that the parameter for saveHTML is available as of PHP 5.3.6.)

    I'd recommend saving the nodes themselves, though, and converting them to string only shortly before you output them.