phpdomdocument

How to count children in DOMNodeList? PHP


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?


Solution

  • The Countable implementation on the DOMNodeList class was added in PHP 7.2, which was released in November 2017:

    Until that was added, using count() just returned the default value it returned for any non-Countable object, which is always 1. (Since PHP 8.0, there is no such default, objects that don't implement Countable return an error instead.)

    This means that you are running a version of PHP which has reached end of life - the last version where this code will not give the correct result is PHP 7.1, which last had an official security release in December 2019.

    If at all possible, you should be upgrading to a supported PHP version. If you are genuinely unable to do that, you need to be aware that a lot of examples you see, and libraries you try to use, will assume you are on a newer PHP version.