3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <div> <div class="coincodex-content"> hello this is content <div class="delete_this" style="margin: 8px 0; clear: both;"> <div> <center><span style="font-size:11px; color: gray;">TEST</span></center> <b>TEST</b><br><br> </div> </div> <div class="preserved"> Test2 </div> </div> </div> HTML; $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $nodelist = $xpath->query( '//*[@class="coincodex-content"]' ); $fragment = []; foreach($nodelist as $contentNode){ $removeNodelist = $xpath->query('//div[@class="delete_this"]',$contentNode); $item = $removeNodelist->item(0); //only first $item->parentNode->removeChild($item); $fragment[] = $doc->saveHTML($contentNode); } echo $fragment[0];
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
<div class="coincodex-content"> hello this is content <div class="preserved"> Test2 </div> </div>

preferences:
133.21 ms | 407 KiB | 5 Q