Parse html using PHP and loop through table rows and columns?

前端 未结 3 772
南笙
南笙 2021-01-19 15:31

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

3条回答
  •  伪装坚强ぢ
    2021-01-19 16:09

    Would re-looping work?

    $DOM->loadHTML($url);
    $rows= $DOM->getElementsByTagName('tr');
    $tds= $DOM->getElementsByTagName('td');
    
    for ($i = 0; $i < $rows->length; $i++) {
    // loop through columns
         for ($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.

提交回复
热议问题