Parse Html Using Php And Loop Through Table Rows And Columns?
I'm trying to parse HTML from loadHTML but I'm having trouble, I managed to loop through all s in the document but I don't know how to loop through the s on ea
Solution 1:
DOMElement
also supports getElementsByTagName
:
$DOM = new DOMDocument();
$DOM->loadHTMLFile("file path or url");
$rows = $DOM->getElementsByTagName("tr");
for ($i = 0; $i < $rows->length; $i++) {
$cols = $rows->item($i)->getElementsbyTagName("td");
for ($j = 0; $j < $cols->length; $j++) {
echo$cols->item($j)->nodeValue, "\t";
// you can also use DOMElement::textContent// echo $cols->item($j)->textContent, "\t";
}
echo"\n";
}
Solution 2:
Use DOMXPath
to query out the child column nodes with a relative xpath query, like this:
$xpath = new DOMXPath( $DOM);
$rows= $xpath->query('//table/tr');
foreach( $rowsas$row) {
$cols = $xpath->query( 'td', $row); // Get the <td> elements that are children of this <tr>foreach( $colsas$col) {
echo$col->textContent;
}
}
Edit: To start at specific rows and stop, keep your own index on the row by changing how you're iterating over the 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( $colsas$col) {
echo$col->textContent;
}
}
Solution 3:
Would re-looping work?
$DOM->loadHTML($url);
$rows= $DOM->getElementsByTagName('tr');
$tds= $DOM->getElementsByTagName('td');
for ($i = 0; $i < $rows->length; $i++) {
// loop through columnsfor ($i = 0; $i < $tds->length; $i++) {
// loop through rows
}
}
EDIT You will also have to check the parent node
to make sure that the rows
parent is the tr
you are currently in. Something like
if ($rows == tds->parent_node){
//do whatever
}
May not be syntactically 100% correct, but the concept is sound.
Post a Comment for "Parse Html Using Php And Loop Through Table Rows And Columns?"