PHP: extra content at the end of the document in entity, trying to extract the nodeValue

KJW picture KJW · Oct 9, 2012 · Viewed 10.6k times · Source
<?php

echo getValue('<a>dk</a><b>sh</b>', 'a');

function getValue($string, $tagname) {
    $dom = new DomDocument();
    $dom->loadXML($string);
    $node_list = $dom->getElementsByTagName($tagname)->item(0);
    return $node_list->nodeValue;
}

running the script returns

Warning: DOMDocument::loadXML(): Extra content at the end of the document in Entity, line: 1 in /Users/johnkim/get.php on line 7

Answer

kirugan picture kirugan · Oct 9, 2012

Well, your xml string is not valid. Try to wrap it with any tag like:

<some_tag><a>sdfsdf</a><b>sdfsdf</b></some_tag>