3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = <<<DATA <div> <p>سلام</p> <p class="myclass">Remove this one</p> <p>But keep this</p> <div style="color: red">and this</div> <div style="color: red">and <p>also</p> this</div> <div style="color: red">and this <div style="color: red">too</div></div> </div> DATA; $dom = new DOMDocument(); $dom->loadHTML(mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); foreach ($xpath->query("//*[@*]") as $node) { $parent = $node->parentNode; while ($node->hasChildNodes()) { $parent->insertBefore($node->lastChild, $node->nextSibling); } $parent->removeChild($node); } echo $dom->saveHTML();
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /in/DqZKu on line 15 <div> <p>&#1587;&#1604;&#1575;&#1605;</p> Remove this one <p>But keep this</p> and this and <p>also</p> this and this too </div>
Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.5 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
<div> <p>&#1587;&#1604;&#1575;&#1605;</p> Remove this one <p>But keep this</p> and this and <p>also</p> this and this too </div>

preferences:
112.97 ms | 409 KiB | 5 Q