Как удалить атрибуты с помощью PHP DOMDocument?

С помощью этого фрагмента XML:

<my_xml> <entities> <image url="lalala.com/img.jpg" id="img1" /> <image url="trololo.com/img.jpg" id="img2" /> </entities> </my_xml> 

Я должен избавиться от всех атрибутов в тегах изображений. Итак, я сделал это:

 <?php $article = <<<XML <my_xml> <entities> <image url="lalala.com/img.jpg" id="img1" /> <image url="trololo.com/img.jpg" id="img2" /> </entities> </my_xml> XML; $doc = new DOMDocument(); $doc->loadXML($article); $dom_article = $doc->documentElement; $entities = $dom_article->getElementsByTagName("entities"); foreach($entities->item(0)->childNodes as $child){ // get the image tags foreach($child->attributes as $att){ // get the attributes $child->removeAttributeNode($att); //remove the attribute } } ?> 

Как-то, когда я пытаюсь удалить атрибут из блока foreach, он выглядит как потерянный внутренний указатель и он не удаляет оба атрибута.

Есть ли другой способ сделать это?

Заранее спасибо.

Измените внутренний цикл foreach на:

 while ($child->hasAttributes()) $child->removeAttributeNode($child->attributes->item(0)); 

Или обратно на переднее удаление:

 if ($child->hasAttributes()) { for ($i = $child->attributes->length - 1; $i >= 0; --$i) $child->removeAttributeNode($child->attributes->item($i)); } 

Или сделать копию списка атрибутов:

 if ($child->hasAttributes()) { foreach (iterator_to_array($child->attributes) as $attr) $child->removeAttributeNode($attr); } 

Любой из них будет работать.