I\'m trying to edit html tags with DOMDocument::loadHTML in php. The html data is a part of html and not the whole page. I followed what this page (PHP - DOMDocument - need
The problem is the call to replaceChild(). Rather than
$dom->replaceChild($nodeDiv, $nodePre);
use
$nodePre->parentNode->replaceChild($nodeDiv, $nodePre);
update
Here is a working code. Seems there is some issue with replacing multiple nodes (more info here: http://php.net/manual/en/domnode.replacechild.php) so you'll have to use a regressive loop to replace the elements.
$contents = <<hi
hello
byeSTR; $dom = new DOMDocument; @$dom->loadHTML($contents); $elements = $dom->getElementsByTagName("pre"); for ($i = $elements->length - 1; $i >= 0; $i --) { $nodePre = $elements->item($i); $nodeDiv = $dom->createElement("div", $nodePre->nodeValue); $nodePre->parentNode->replaceChild($nodeDiv, $nodePre); }