3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * How to get attribute of node with namespace using SimpleXML? * * @link http://stackoverflow.com/q/6576773/367456 */ $buffer = '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <entry> <yt:duration seconds="1870"/> </entry> </feed>'; $xml = new SimpleXMLElement($buffer); echo "ibxml version: ", LIBXML_DOTTED_VERSION, "\n"; foreach ($xml->entry as $item) { //original comment: how to get seconds? $namespaces = $item->getNameSpaces(true); var_dump($namespaces); $yt = $item->children($namespaces['yt']); $seconds = $yt->duration->attributes(); echo $seconds['seconds'], "\n"; // original comment: but doesn't work. } $test = array("yt:second" => 124); var_dump($test); echo "done. should read 1870 one time.\n";
Output for 5.5.36 - 5.5.38, 5.6.22 - 5.6.28, 7.0.7 - 7.1.0
ibxml version: 2.9.4 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.21, 7.0.0 - 7.0.6
ibxml version: 2.9.3 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.4.0 - 5.4.45
ibxml version: 2.9.2 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.3.25 - 5.3.29
ibxml version: 2.9.1 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.3.15 - 5.3.24
ibxml version: 2.8.0 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.1.2 - 5.3.14
ibxml version: 2.7.8 array(2) { [""]=> string(27) "http://www.w3.org/2005/Atom" ["yt"]=> string(37) "http://gdata.youtube.com/schemas/2007" } 1870 array(1) { ["yt:second"]=> int(124) } done. should read 1870 one time.
Output for 5.1.0 - 5.1.1
ibxml version: 2.7.8 Fatal error: Call to undefined method SimpleXMLElement::getNameSpaces() in /in/fKmjP on line 20
Process exited with code 255.
Output for 5.0.0 - 5.0.5
ibxml version: Notice: Use of undefined constant LIBXML_DOTTED_VERSION - assumed 'LIBXML_DOTTED_VERSION' in /in/fKmjP on line 15 LIBXML_DOTTED_VERSION Fatal error: Call to undefined method SimpleXMLElement::getNameSpaces() in /in/fKmjP on line 20
Process exited with code 255.
Output for 4.3.2 - 4.4.9
Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/fKmjP on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/fKmjP on line 13