run code in 200+ PHP versions simultaneously
<?php $html = <<<HTML <html> <body> <div class="regular-box">DIV 0</div> <div class="comments-box">DIV 1</div> <div class="heycomments-box">DIV 2</div> <div class="sdfcomments-box">DIV 3</div> <div class="sdgsdgcomments-boxsdfdfh">DIV 4</div> </body> </html> HTML; $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); $xp = new DOMXPath($dom); // get the node list of div nodes with "target" class $spanNodeList = $xp->query('//div[contains(@class, "comments-box")]'); foreach ($spanNodeList as $spanNode) { $spanNode->parentNode->replaceChild($spanNode->firstChild, $spanNode); } echo $dom->saveHTML();
Output for 7.1.25 - 7.4.0
<html> <body> <div class="regular-box">DIV 0</div> DIV 1 DIV 2 DIV 3 DIV 4 </body> </html>