note 94346 added to class.domnamednodemap

From: Date: Thu, 29 Oct 2009 15:55:34 +0000
Subject: note 94346 added to class.domnamednodemap
Groups: php.notes 
Request: Send a blank email to php-notes+get-162257@lists.php.net to get a copy of this message
To add to xafford's comment. When iterating a named node map collection using ->item() or using foreach, removing a attribute with DOMNode->removeAttribute() or DOMNode->removeAttributeNode() alters the collection as if it were a stack. To illustrate, the code below tries to remove all attributes from each element but only removes the first. One work around is to copy the named node map into an array before removing attributes. Using PHP 5.2.9 on Windows XP. <?php error_reporting(E_ALL); $html = '<h1 id="h1test" class="h1test">Heading</h1>'; $html .= '<p align="left" class="ptest">Hello World</p>'; $doc = new DOMDocument(); $doc->loadHTML($html); // remove attributes from the h1 element $h1 = $doc->getElementsByTagName('h1')->item(0); $length = $h1->attributes->length; for ($i = 0; $i < $length; ++$i) { $name = $h1->attributes->item($i)->name; $h1->removeAttribute($name); echo "h1: removed attribute $name<br>"; } // remove attributes from the p element $p = $doc->getElementsByTagName('p')->item(0); foreach ($p->attributes as $name => $attrNode) { $p->removeAttribute($name); echo "p: removed attribute $name<br>"; } ?> OUTPUT: ------- h1: removed attribute id Notice: Trying to get property of non-object in nodemap.php on line 13 h1: removed attribute `` p: removed attribute align ---- Server IP: 69.147.83.197 Probable Submitter: 64.244.102.2 ---- Manual Page -- http://www.php.net/manual/en/class.domnamednodemap.php Edit -- https://master.php.net/note/edit/94346 Del: integrated -- https://master.php.net/note/delete/94346/integrated Del: useless -- https://master.php.net/note/delete/94346/useless Del: bad code -- https://master.php.net/note/delete/94346/bad+code Del: spam -- https://master.php.net/note/delete/94346/spam Del: non-english -- https://master.php.net/note/delete/94346/non-english Del: in docs -- https://master.php.net/note/delete/94346/in+docs Del: other reasons-- https://master.php.net/note/delete/94346 Reject -- https://master.php.net/note/reject/94346 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162257) next »