This code snippet shows a function that will load some xml (ensure that at least tags opened have a closing pendant and such, otherwise you will see / read some errors) and then applies htmlentities onto all text-nodes. I actually have no real clue for what you need that, but probably it makes you happy:
$foo = '<div class="link">Here\'s is a link: <a href="http://www.example.com">Doors & windows</a></div>';
echo text_htmlentities(utf8_encode($foo));
/**
* add htmlentities onto the text-nodes of an
* xml fragment.
*
* @param string $foo xml fragment (utf8)
* @return string
*/
function text_htmlentities($foo) {
$foo = str_replace('&', '&', $foo);
$dom = new DOMDocument;
$dom->loadXml($foo);
$xpath = new DomXpath($dom);
foreach($xpath->query('//text()') as $node) {
$node->nodeValue = htmlentities($node->nodeValue, ENT_QUOTES, 'UTF-8', false);
}
return str_replace('&','&', $dom->saveXml($dom->firstChild));
}
output:
<div class="link">Here's is a link: <a href="http://www.example.com">Doors & windows</a></div>
$out = "<div class=\"link\" etc....anyways, otherwise both of your versions are syntax errors.htmlentitieson the data inside of those elements.