<?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();
You have javascript disabled. You will not be able to edit any code.