1
$html = new DOMDocument();
           $html->loadHTMLFile($filename);

           $meta = $html->getElementsByTagName("meta");


           foreach($meta as $oldmeta_tags)
           {

               $parent = $oldmeta_tags->parentNode;
               $parent->removeChild($oldmeta_tags);

           }
         echo "<br>Number of bytes stored = ".$html->saveHTMLFile($filename);
           $result[] = file_get_contents($filename);

Some of the meta tags are removed and some are not. please help what i am doing wrong

Khurram Ijaz
  • 1,844
  • 5
  • 24
  • 43
  • *(related)* [DOMNode replacement with PHP's DOM classes](http://stackoverflow.com/questions/4615661/domnode-replacement-with-phps-dom-classes) - it's the same answer, but since this is not as obvious as your other three questions, I wont closevote it. And see, you **almost did it** on your own from the docs and the examples ;) – Gordon Apr 21 '11 at 12:52

2 Answers2

4

When you use foreach to iterate over the DOMNodeList and remove an element, you are changing the DOMNodeList content, so nodes will be skipped. You have to iterate backwards:

$nodes = $dom->getElementsByTagName('meta');
for ($i = $nodes->length - 1; $i >= 0; $i--) {
    $nodes->item($i)->parentNode->removeChild($nodes->item($i));
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

You're looping over the array and removing from it at the same time.

Unfortunately, this means that every time you remove a child inside the loop, the next loop iteration skips a node. foreach is not "clever enough" in conjunction with DOMDocument to do this intelligently.

Instead of foreach, use indexes:

$meta = $html->getElementsByTagName("meta");
for ($i = $meta->length - 1; $i >= 0; $i--) { // `foreach` breaks the `removeChild`
   $child = $meta->item($i);
   $child->parentNode->removeChild($child);
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055