0

how do i return more than one node on a page? here is the code i am using, but it only returns the 1st node found.

$dom = new DOMDocument;
$dom->loadHTMLFile($URL);

$xpath  = new DOMXpath($dom);
$users= $xpath->query('//b[@class="xc_user"]');

echo $users->item(0)->nodeValue;
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
sarsar
  • 1,431
  • 4
  • 14
  • 17
  • 1
    Use `foreach` to iterate the returned [`DOMNodeList`](http://de.php.net/manual/en/class.domnodelist.php) or [any other loop control structure.](http://de.php.net/manual/en/language.control-structures.php) – Gordon May 11 '11 at 21:45
  • @Gordon I tried this `foreach ($users->item->nodeValue as $result) { echo $result; }` no luck is this in the right direction? – sarsar May 11 '11 at 21:53
  • Just `foreach ($user as $node)`. This will return a `DOMElement` for `$node`. See my answer to http://stackoverflow.com/questions/5249492/xpath-not-behaving-for-me-in-parsing-basic-html/5249675#5249675 – Gordon May 11 '11 at 21:55

1 Answers1

1

$users is a DOMNodeList, which you can iterate over:

foreach($users as $node) {
    echo $node->nodeValue;
}
Marc B
  • 356,200
  • 43
  • 426
  • 500