简体   繁体   English

php domdocument 或 domxpath:如何提取 TR 并保存 html

[英]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.我有一个字符串中的 html 表。

<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. <TD>内部是一些我需要使用的<DIV > 和<SPAN>

I need to extract each <TR> (both classes) and save the html in an array where each <TR> is an array element.我需要提取每个<TR> (两个类)并将html保存在一个数组中,其中每个<TR>都是一个数组元素。

Creating a node list array is easy enough, but how do I get the actual html?创建节点列表数组很容易,但我如何获得实际的 html?

If you must save the HTML as a string, there is DOMDocument::saveHTML如果必须将 HTML 保存为字符串,则有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.) (请注意, saveHTML的参数自 PHP 5.3.6 起可用。)

I'd recommend saving the nodes themselves, though, and converting them to string only shortly before you output them.不过,我建议保存节点本身,并仅在您 output 它们之前不久将它们转换为字符串。

Alternatively using DOMDocument only:或者仅使用DOMDocument

$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);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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