简体   繁体   中英

DOMXPath/DOMDocument - How to fetch foreach of many elements with same attribute

I am using DOMXPath and DOMDocument to fetch data from HTML output source.

Here is my code:

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$name = $xpath->query('//td[@data-column-name="Model"]')->item(0)->nodeValue;

When i echo $name i receive only the first result which this code is finding. There are much more elements with <td class=" " data-column-name="Model"> but this code is giving me only the first result. Do i have to make any foreach or while loop and how to get all results ?

Thanks in advance!

Use a loop like:

foreach ($xpath->query('//td[@data-column-name="Model"]')) as $item) {
    echo $item->nodeValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM