$allowedAttributes = ['src', 'href'];
foreach($elements as $element) {
for($i = $element->attributes->length; --$i >= 0;) {
if(!in_array($element->attributes->item($i)->name, $allowedAttributes)) {
$element->removeAttribute($element->attributes->item($i)->name);
}
}
}
echo $dom->saveHTML();
$allowedAttributes = ['src', 'href'];
foreach($elements as $element) {
if( $element->attributes ) {
for($i = $element->attributes->length; --$i >= 0;) {
if(!in_array($element->attributes->item($i)->name, $allowedAttributes)) {
$element->removeAttribute($element->attributes->item($i)->name);
}
}
}
}
echo $dom->saveHTML();