note 98444 modified in simplexmlelement.count by danbrown

From: Date: Fri, 25 Jun 2010 15:26:01 +0000
Subject: note 98444 modified in simplexmlelement.count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170461@lists.php.net to get a copy of this message
I used the DOMNodeList $length property to simulate this function since my host had not yet implemented PHP 5.3. The following code makes it portable and allows you to prepare for an upgrade path. <?php $url = "http://www.example.com/foo.xml"; $xml = simplexml_load_file($url); // For debugging // echo "<p>".phpversion()."</p>"; if(phpversion() >= '5.3.0'){ $bar_count = $xml->foo->bar->count(); } else { $doc = new DOMDocument(); $str = $xml->asXML(); $doc->loadXML($str); $bar_count = $doc->getElementsByTagName("bar")->length; } echo "There are ".$bar_count." \"bar\" nodes in this document.<br />"; ?> --was-- Apologize...above code used wrong escape sequence for literal double quotes. Corrected below... <?php $url = "http://www.example.com/foo.xml"; $xml = simplexml_load_file($url); // For debugging // echo "<p>".phpversion()."</p>"; if(phpversion() >= '5.3.0'){ $bar_count = $xml->foo->bar->count(); } else { $doc = new DOMDocument(); $str = $xml->asXML(); $doc->loadXML($str); $bar_count = $doc->getElementsByTagName("bar")->length; } echo "There are ".$bar_count." \"bar\" nodes in this document.<br />"; ?> http://php.net/manual/en/simplexmlelement.count.php

« previous php.notes (#170461) next »