I’m attempting to replace all <foo> elements in a DOMDocument with <bar> elements. The best way to do this seems to be to grab all the <foo> elements with getElementsByTagName and then create new elements to replace these with.
The problem is, I can’t see to grab the innerHTML from the element which is being replaced. nodeValue and textContent both return the inner text, but the inner tags get stripped away. How can a I get something similar to “innerHTML” from a DOMElement?
Sample code:
function convertTags($doc, $type) {
$bad_nodes = $doc->getElementsByTagName($type);
foreach($bad_nodes as $bad_node) {
$good_node = $doc->createElement("bar", $bad_node->nodeValue);
$bad_node->parentNode->replaceChild($good_node, $bad_node);
}
return $doc;
}
I think you could do better by treating them like actual nodes: