[PHP] Dom appendChild strips trailing text from within a node
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='div1 bold1 italic1underline1 bold2 div2bold3 underline'; $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)) . ''; echo nl2br(htmlentities(str_replace(array('', '', ''), '', strstr($dom->saveXML(), ''; /* Should return: div1 bold1 italic1underline1 bold2 div2bold3 underline Instead returns: div1 bold1 italic1underline1 div2bold3 underline */
[PHP] How to clone an entire XML node including childnodes?
Basically I am trying to turn something like this: Hello Joe, nice to meet you into this: Hello Joe, nice to meet you by making a new node, inserting before the old node and then removing the old node. The problem is that I am populating the value of the new node with the $oldNode->nodeValue property which apparently only includes text, not child nodes, so I end up with: Hello Joe, nice to meet you How do you get the entire node value including any subnodes that may appear within? This is basically a variation on my previous post about how to DomDocument->renameNode().
[PHP] Re: How can I DomDocument->renameNode?
Thanks for the reply, but the solution needs to be in PHP, not JavaScript. -Original Message- From: tedd [mailto:[EMAIL PROTECTED] Sent: Sunday, June 03, 2007 3:20 PM To: php-general@lists.php.net Subject: [PHP] Re: How can I DomDocument->renameNode? >I have tried making a new node, inserting it before the old node then >removing the old node but I could not figure out how to get the >nodeValue to include the child nodes, so they get stripped out leaving >only the text value. You might try something like this: function replaceNode() { var inChoice = document.getElementById("grafCount").selectedIndex; var inText = document.getElementById("textArea").value; var newText = document.createTextNode(inText); var newGraf = document.createElement("p"); newGraf.appendChild(newText); var allGrafs = nodeChangingArea.getElementsByTagName("p"); var oldGraf = allGrafs.item(inChoice); nodeChangingArea.replaceChild(newGraf,oldGraf); } tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How can I DomDocument->renameNode?
Apparently renameNode is not yet implemented into Dom, so how can I rename a node without affecting its value and/or child nodes? I have tried making a new node, inserting it before the old node then removing the old node but I could not figure out how to get the nodeValue to include the child nodes, so they get stripped out leaving only the text value.