3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<'HTML' <div id='makeme' class='testme'> <span id='whatspanID'>somthing</span> <p class='ptagclass'></p> </div> HTML; $dom = new DOMDocument(); $dom->loadHtml($html); $xpath = new DOMXpath($dom); $div = $xpath->evaluate('//div[@id="makeme"]')->item(0); $nodes = $xpath->evaluate('.//*/@*[not(local-name() = "id")]', $div); $length = count($nodes); for($i = $length - 1; $i >= 0; --$i) { $node = $nodes->item($i); var_dump($node); $node->parentNode->removeChild($node); } echo $dom->saveHtml($div);

preferences:
32.77 ms | 402 KiB | 5 Q