简体   繁体   中英

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?

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.

Alternatively using DOMDocument only:

$dom = new DOMDocument();   
@$dom->loadHTML($html);

if($table=$dom->getElementsByTagName('table')->item(0)){

    //traverse the table and output every rows

    $rows=array();
    foreach ($table->childNodes as $row){

        $rows[]=$dom->saveHTML($row);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM