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

前端 未结 3 807
南笙
南笙 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:18

    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";
    }
    

提交回复
热议问题