I am trying to count children of a DOMNode
. It appears to give wrong results when I use count
vs traversing or the ->length
property. As if text nodes are not counted.
Consider
<?php
$html = '<p>with <a>link</a> text</p>';
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$paragraph = $dom->firstChild;
$count = 0;
foreach ($paragraph->childNodes as $childNode) {
$count++;
}
echo count($paragraph->childNodes) . "n";
echo $paragraph->childNodes->length . "n";
echo $count . "n";
And the output is:
1
3
3
Is this a bug in Countable
implementation of DOMNodeList
?