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