How to clear all tags from an HTML string from files that do not have a jpg or gif extension in PHP
I have to remove all img tags from a string, where the extension of the src is not jpg or gif.
So far I tried:
$dom=new domDocument; $dom->loadHTML($content); $dom->preserveWhiteSpace=false; $images=$dom->getElementsByTagName('img'); foreach($images as $image) { $ea=array('gif','png','bmp','jpg','jpeg'); $ext=pathinfo($image->getAttribute('src'), PATHINFO_EXTENSION); if(!in_array(strtolower($ext),$ea)) { $image->removeAttribute('src'); } } $cleanc=$dom->saveHTML();
but this is slow and it only removes the source.
Advertisement
Answer
Well, DOMDocument is not going to be particularly fast. If you want to remove the image and not just its src, try:
if(!in_array(strtolower($ext),$ea)) { $parent = $image->->parentNode; $parent->removeChild($image); }