你的XML字符串无效,则必须在text节点转换content到ヶ辆第一,例如:
$textContent = htmlentities($text);
在那之后,我们有:
$xmlText = '
<p>HTML content 1</p>
<q>HTML<q> content 2
HTML <b>content 3<b>
';
现在,我们只需要请使用SimpleXMLElement来解析:
$xmlObject = new SimpleXMLElement($xmlText);
$items = $xmlObject->xpath("text");
foreach ($items as $item){
echo html_entity_decode($item);
}
更新1
如果你不能改变你的XML字符串,你需要使用正则表达式,而不是htmlDom:
function get_tag_contents($tag, $xml) {
preg_match_all("#(.*?)$tag>#", $xml, $matches);
return $matches[1];
}
$invalidXml = '
HTML content 1
HTML
content 2
HTML content 3
';
$textContents = get_tag_contents('text', $invalidXml);
foreach ($textContents as $content) {
echo $content;
}