3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = <<<DATA <div> <p>These line shall stay</p> <p class="myclass">Remove this one</p> <p>But keep this</p> <div style="color: red">and this</div> </div> DATA; $dom = new DOMDOcument(); $dom->loadHTML($data, LIBXML_HTML_NOIMPLIED); $dom->removeChild($dom->doctype); $xpath = new DOMXPath($dom); $lines_to_be_removed = $xpath->query("//*[count(@*)>0]"); foreach ($lines_to_be_removed as $line) { $line->parentNode->removeChild($line); } // just to check echo $dom->saveHTML(); ?>
Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.15, 8.1.0 - 8.1.2
<div> <p>These line shall stay</p> <p>But keep this</p> </div>

preferences:
42.7 ms | 461 KiB | 5 Q