3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $xml = <<<'XML' <nitf:body.content xmlns:nitf="foo"> <nitf:block> <nitf:p style="@style1">Contents of paragraph1.</nitf:p> <nitf:p style="@style2">Contents of paragraph2.</nitf:p> <nitf:p style="@style1"><nitf:em class="@bold">This is bold</nitf:em> This is not bold</nitf:p> <nitf:p style="@style1"><nitf:em class="@italic">This is italic</nitf:em> This is not italic</nitf:p> </nitf:block> </nitf:body.content> XML; $nitfNS = "foo"; $doc = new DOMDocument(); $doc->loadXML($xml); foreach($doc->getElementsByTagNameNS($nitfNS, 'em') as $em) { $class = $em->getAttribute('class'); if ($class == '@italic') { $em->nodeValue = '<i>' . $em->nodeValue . '</i>'; } elseif (strpos($class, 'bold') !== FALSE) { $em->nodeValue = '<b>' . $em->nodeValue . '</b>'; } echo $doc->saveXML($em), "\n"; } foreach ($doc->getElementsByTagNameNS($nitfNS, 'p') as $element) { $textnode = $element->childNodes->item(0); echo $textnode->textContent, "\n"; } echo $doc->saveXML();
Output for 5.6.0 - 7.2.6
<nitf:em class="@bold">&lt;b&gt;This is bold&lt;/b&gt;</nitf:em> <nitf:em class="@italic">&lt;i&gt;This is italic&lt;/i&gt;</nitf:em> Contents of paragraph1. Contents of paragraph2. <b>This is bold</b> <i>This is italic</i> <?xml version="1.0"?> <nitf:body.content xmlns:nitf="foo"> <nitf:block> <nitf:p style="@style1">Contents of paragraph1.</nitf:p> <nitf:p style="@style2">Contents of paragraph2.</nitf:p> <nitf:p style="@style1"><nitf:em class="@bold">&lt;b&gt;This is bold&lt;/b&gt;</nitf:em> This is not bold</nitf:p> <nitf:p style="@style1"><nitf:em class="@italic">&lt;i&gt;This is italic&lt;/i&gt;</nitf:em> This is not italic</nitf:p> </nitf:block> </nitf:body.content>