用于DOMXPath
通过相对 xpath 查询查询出子列节点,如下所示:
$xpath = new DOMXPath( $DOM);
$rows= $xpath->query('//table/tr');
foreach( $rows as $row) {
$cols = $xpath->query( 'td', $row); // Get the <td> elements that are children of this <tr>
foreach( $cols as $col) {
echo $col->textContent;
}
}
编辑:要从特定行开始并停止,请通过更改迭代的方式在行上保留自己的索引DOMNodeList
:
$xpath = new DOMXPath( $DOM);
$rows= $xpath->query('//table/tr');
for( $i = 3, $max = $rows->length - 2; $i < $max, $i++) {
$row = $rows->item( $i);
$cols = $xpath->query( 'td', $row);
foreach( $cols as $col) {
echo $col->textContent;
}
}