I am trying to remove all images from an HTML string. I can only remove the first one and I don't know why.
the code:
<?php
$str='<div>
<a href=
"https://www.google.com">
<img src=
"image1.jpg"
alt="image-1.jpg" /></a>
</div>
<p>
hobby\'s vs hobbies
</p>
<div>
<a href=
"https://www.google.com">
<img src=
"image2.jpg"
alt="image-2.jpg" /></a>
</div>';
$dom=new domDocument;
$dom->loadHTML($str);
$images=$dom->getElementsByTagName('img');
foreach($images as $image)
{
$image->parentNode->removeChild($image);
}
$result=$dom->saveHTML();
echo '<textarea>'.$result.'</textarea>';
?>
Aucun commentaire:
Enregistrer un commentaire