3v4l.org

run code in 500+ PHP versions simultaneously
<?php $filedata = <<<EOT <?xml version="1.0" encoding="utf-8" ?> <article xmlns="http://docbook.org/ns/docbook" version="5.0" xmlns:xlink="http://www.w3.org/1999/xlink" > <para> This is an <emphasis role="strong">hello <em>world</em></emphasis> sentence. </para> </article> EOT; $dom = new DOMDocument(); $dom->loadXML($filedata); foreach($dom->documentElement->childNodes as $node){ if(XML_ELEMENT_NODE === $node->nodeType && 'para' === $node->nodeName){ // Replace any emphasis elements foreach($node->childNodes as $childNode) { if(XML_ELEMENT_NODE === $childNode->nodeType && 'emphasis' === $childNode->nodeName){ // This is arguably the most "correct" way to replace, just in case // there's extra nodes inside. A cheaper way would be to not loop // and just use the nodeValue however you might lose some HTML. $newNode = $dom->createElement('b'); foreach($childNode->childNodes as $grandChild){ $newNode->appendChild($grandChild->cloneNode(true)); } $childNode->replaceWith($newNode); } } // Build our output $output = ''; foreach($node->childNodes as $childNode) { $output .= $dom->saveHTML($childNode); } // The provided XML has a namespace, and when cloning nodes that NS comes // along. Since we are going from regular XML to irregular HTML I think // a string replacement is best. $output = str_replace(' xmlns="http://docbook.org/ns/docbook"', '', $output); echo $output; } }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.18, 8.1.21 - 8.1.34, 8.2.0 - 8.2.5, 8.2.8 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
This is an <b>hello <em>world</em></b> sentence.
Output for 8.1.19 - 8.1.20, 8.2.6 - 8.2.7
Fatal error: Uncaught DOMException: Not Found Error in /in/04Tc3:31 Stack trace: #0 /in/04Tc3(31): DOMElement->replaceWith(Object(DOMElement)) #1 {main} thrown in /in/04Tc3 on line 31
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Call to undefined method DOMElement::replaceWith() in /in/04Tc3:31 Stack trace: #0 {main} thrown in /in/04Tc3 on line 31
Process exited with code 255.

preferences:
89.97 ms | 1257 KiB | 4 Q