0

我被这个困住了。我尝试使用 php dom 来解析一些 html 代码。我如何才能知道我在 for 循环中迭代的当前元素有多少个子元素?

<?php
$str='
<table id="tableId">
<tr>
    <td>row1 cell1</td>
    <td>row1 cell2</td>
</tr>
<tr>
    <td>row2 cell1</td>
    <td>row2 cell2</td>
</tr>
</table>
';

$DOM = new DOMDocument;
$DOM->loadHTML($str);   // loading page contents
$table = $DOM->getElementById('tableId');   // getting the table that I need
$DOM->loadHTML($table);     

$tr = $DOM->getElementsByTagName('tr');     // getting rows

echo $tr->item(0)->nodeValue;   // outputs row1 cell1 row1 cell2 - exactly as I expect with both rows
echo "<br>";
echo $tr->item(1)->nodeValue;   // outputs row2 cell1 row2 cell2

// now I need to iterate through each row to build an array with cells that it has
for ($i = 0; $i < $tr->length; $i++)
{
echo $tr->item($i)->length;     // outputs no value. But how can I get it?
echo $i."<br />";
}
?>
4

1 回答 1

2

这将为您提供所有子节点

$tr->item($i)->childNodes->length;

...但是:它将包含DOMText带有空格等的节点(因此计数为 4)。如果您不一定需要长度,只想遍历所有节点,您可以这样做:

foreach($tr->item($i)->childNodes as $node){
    if($node instanceof DOMElement){
        var_dump($node->ownerDocument->saveXML($node));
    }
}

如果您只需要一定长度的元素,您可以这样做:

$x = new DOMXPath($DOM);
var_dump($x->evaluate('count(*)',$tr->item($i)));

你可以这样做:

foreach($x->query('*',$tr->item($i)) as $child){
    var_dump($child->nodeValue);
}

通过 foreach-ing->childNodes我更喜欢简单的“数组构建”。请记住,您只需foreach通过DOMNodeList's 就好像它们是数组一样,可以节省很多麻烦。

从表中构建一个简单的数组:

$DOM = new DOMDocument;
$DOM->loadHTML($str);   // loading page contents
$table = $DOM->getElementById('tableId'); 
$result = array();
foreach($table->childNodes as $row){
   if(strtolower($row->tagName) != 'tr') continue;
   $rowdata = array();
   foreach($row->childNodes as $cell){
       if(strtolower($cell->tagName) != 'td') continue;
       $rowdata[] = $cell->textContent;
   }
   $result[] = $rowdata;
}
var_dump($result);
于 2012-06-14T20:29:41.290 回答