I have a DOMDocument object and I want to manipulate text nodes and return them to the object as a HTML string. For example:
$dom=new DomDocument();
$dom->loadHTML('<html><body>This is [b]BBC[/b]</body></html>');
// $node is a text node
$html=parseBBC($node->nodeValue);
$frag=$dom->createDocumentFragment();
$frag->appendXML($html);
$node->parentNode->replaceChild($frag,$node);
echo $dom->saveHTML();
Expected output:
<html><body>This is <b>BBC</b></body></html>
Actual output:
empty string
Edit:
$dom=new DomDocument();
$dom->loadHTML('<html><body>This is[br]BBC</body></html>');
$xpath=new DOMXPath($dom);
$r=$xpath->query('//text()');
foreach($r as $el){
$str=trim(str_replace('[br]',"\n",$el->nodeValue));
$str='<p>'.preg_replace('/(\n|\r)+/','</p><p>',$str).'</p>';
$frag=$dom->createDocumentFragment();
$frag->appendXML($str);
$el->parentNode->replaceChild($frag,$el);
}
echo $dom->saveHTML();
This is
BBC
`. – Alf Eaton Nov 05 '13 at 13:41