3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<HTML <p id="paragraph" class="green"> This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/> </p> HTML; $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); foreach ($xpath->query('//@*[not(name()="src")]') as $attr) { $attr->parentNode->removeAttribute($attr->nodeName); } echo $dom->saveHTML();
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
<p> This is a paragraph with an image <img src="/path/to/image.jpg"> </p>

preferences:
97.67 ms | 1397 KiB | 4 Q