8
<?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;
}

运行脚本返回

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

1 回答 1

15

好吧,您的 xml 字符串无效。尝试用任何标签包装它,例如:

<some_tag><a>sdfsdf</a><b>sdfsdf</b></some_tag>
于 2012-10-09T19:09:47.600 回答