How to iterate through elements in DOMNodeList?

I found iterating over even a moderately a large DOMNodeList to be very slow using foreach(). A much faster method is to use the DOMNode $nextSibling property in do-while loop like this:

$el = $paragraph->firstChild;
do {
    // do stuff
} while ($el = $el->nextSibling);

This is also mentioned in a comment on php.net here.


DOMNodeList implements Traversable, just use foreach()

foreach($nodeList as $node) {
  //...
}

Of course a for is possible, too.

$length = $nodeList->length;
for ($i = 0; $i < $length; $i++) {
  $node = $nodeList->item($i);
  //...
}

To get all the text content inside a node, the $nodeValue or $textContent properties can be used:

$text = '';
foreach($nodeList as $node) {
  $text .= $node->textContent;
}

But this is for a node list. You said that this is the text content of a paragraph. If you have the paragraph as an DOMElement object, it has $nodeValue and $textContent properties as well.

$text = $paragraphNode->textContent;

And if you fetched the nodes via Xpath, DOMXpath::evaluate() can return the text content as a string.

$xpath = new DOMXpath($dom);
$text = $xpath->evaluate('string(//p[1])');

Tags:

Php

Loops

Dom