19 April 2013

This snippet shows how to remove HTML elements using DOMDocument in PHP. The problem is that you can't do it in the same loop.

Source code viewer
  1. $remove_elements = array();
  2. $nodes = $dom->getElementsByTagName('td');
  3. foreach ($nodes as $node) {
  4. foreach ($node->attributes as $attribute) {
  5. if ($attribute->name == 'class') {
  6. $remove_elements[] = $node;
  7. }
  8. }
  9. }
  10. foreach ($remove_elements as $element) {
  11. $element->parentNode->removeChild($element);
  12. }
Programming Language: PHP