vendredi 20 février 2015

how to clear all non-jpg img tags from a string

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.


Aucun commentaire:

Enregistrer un commentaire