title: How to remove specific dom element with PHP DOMDocument
body: <p>I have this html in my database:</p>
  
  <pre><code>&lt;p&gt;some text 1&lt;/p&gt;
  &lt;img src=\"http://www.example.com/images/some_image_1.jpg\"&gt;
  &lt;p&gt;some text 2&lt;/p&gt;
  &lt;p&gt;some text 3&lt;/p&gt;
  &lt;img src=\"http://www.example.com/images/some_image_2.jpg\"&gt;
  &lt;p&gt;some text 4&lt;/p&gt;
  &lt;p&gt;some text 5&lt;/p&gt;
  &lt;img src=\"http://www.example.com/images/some_image_3.jpg\"&gt;
  </code></pre>
  
  <p>Conditionally, I need to remove some specific <code>&lt;img&gt;</code> tag. So I don't want to remove all <code>&lt;img&gt;</code> tags, but only specific ones.</p>
  
  <p>I have tried this, but it will remove all <code>&lt;img&gt;</code> tags, even if I do not want that:</p>
  
  <pre><code>$dom = new \DOMDocument;
  $dom-&gt;preserveWhiteSpace = false;
  $dom-&gt;loadHTML($html);
  
  $nodes = $dom-&gt;getElementsByTagName("img");
  
  for($i = 0; $i &lt; $nodes-&gt;length; $i++) {
      if ($i == 1) {
          continue;
      }
      $image = $nodes-&gt;item($i);
      $image-&gt;parentNode-&gt;removeChild($image);
  }
  
  return $dom-&gt;saveHTML();
  </code></pre>
  
  <p>Can someone help me with this ? In this html example, let's say that I want to remove first and third image in text, but to leave second one.</p>
  
  <p>Also, I have noticed that <code>saveHTML()</code> method is adding <code>&lt;html&gt;&lt;body&gt;</code> tags to my html, and I do not want that. I don't see any option to turn this off. Any help there too ?</p>
  
  <p>Thanks in advance, I'm stuck with this for hours.</p>