title: PHP DOMDocument: Delete elements by class
body: <p>I' trying to delete every node with a given class.</p>
  
  <p>To find the elements I use:</p>
  
  <pre><code>$xpath = new DOMXPath($dom);
      foreach( $xpath-&gt;query('//div[contains(attribute::class, "foo")]') as $e ) {
          // Delete this node
      }
  </code></pre>
  
  <p>But how can I delete the elements in this foreach-loop?</p>
  
  <p><strong>Edit:</strong> By the way: How can I check first if there is a element with the class "foo" in the DOM (before starting the loop)?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This is my HTML:</p>
  
  <pre><code>&lt;div class="main"&gt;
      &lt;div class="delete_this" contenteditable="true"&gt;Target&lt;/div&gt;
      &lt;div class="class1"&gt;&lt;/div&gt;
      &lt;div class="content"&gt;&lt;p&gt;Anything&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This doesn't work for the example above:</p>
  
  <pre><code>$xpath = new DOMXPath($dom);
  foreach( $xpath-&gt;query('//div[contains(attribute::class, "delete_this")]') as $e ) {
      $e-&gt;parentNode-&gt;removeChild($e);
  }
  </code></pre>