I am trying to modify a node name and it mostly works, except that appendChild seems to strip the text that FOLLOWS after a subnode. Leading text and subnodes appear to be retained perfectly, just not text trailing the subnode. I tried using cloneNode, but that discarded the children even when I sent "true" as an argument. $html='<div>div1 <b>bold1 <i>italic1<u>underline1</u></i> bold2</b> div2<b>bold3</b> <u>underline</u></div>'; $dom = new DomDocument; $dom->loadHTML($html); $nodes = $dom->getElementsByTagName('b'); foreach ($nodes as $oldNode) { $newNode = $dom->createElement('strong'); foreach($oldNode->childNodes as $thisOldNode) { if ($thisOldNode->nodeName == '#text') { $newNode->nodeValue .= $thisOldNode->nodeValue; } else { // ---- appendChild seems to cause the issue ---- // $newNode->appendChild($thisOldNode); } } $oldNode->parentNode->replaceChild($newNode, $oldNode); } //for debugging: echo nl2br(htmlentities($html)) . '<hr />'; echo nl2br(htmlentities(str_replace(array('<body>', '</body>', '</html>'), '', strstr($dom->saveXML(), '<body>')))); /* Should return: <div>div1 <strong>bold1 <i>italic1<u>underline1</u></i> bold2</strong> div2<b>bold3</b> <u>underline</u></div> Instead returns: <div>div1 <strong>bold1 <i>italic1<u>underline1</u></i></strong> div2<b>bold3</b> <u>underline</u></div> */