我坚持这个。 我尝试使用php dom来解析一些HTML代码。 我怎样才能知道当前元素中有多少个孩子在循环中迭代?
<?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 />";
}
?>
答案 0 :(得分:2)
这将为您提供所有childnodes:
$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);
}
通过->childNodes
进行预测我更喜欢简单的'阵列构建'。请记住,你只需要foreach
到DOMNodeList
,就好像它们是数组一样,可以省去很多麻烦。
从表格构建一个简单的数组:
$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);