Venelin
Venelin

Reputation: 3308

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!

Upvotes: 1

Views: 1189

Answers (1)

Hans Z.
Hans Z.

Reputation: 53928

Use a loop like:

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

Upvotes: 2

Related Questions