note 67205 rejected from function.dom-domdocument-getelementbyid by didou
| From: | didou@php.net | Date: | Sat, 30 Dec 2006 03:13:01 +0000 |
| Subject: | note 67205 rejected from function.dom-domdocument-getelementbyid by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121046@lists.php.net to get a copy of this message | ||
Note Submitter: richard at 2006 dot atterer dot net
----
OK, so maybe PHP's getElementById() is behaving according to spec, but it does not have the
behaviour that people *want*...
Below is a global getElementById() function (i.e. not a DOMDocument method) which recursively
searches through a document for a certain id value.
/* Find the first element in the document (sub)tree whose id attribute
has the value $id. By default, the entire document is searched - pass
a non-NULL value in $node to search only the subtree below $node. */
function getElementById(DOMDocument $doc, /*string*/ $id,
DOMNode $node = NULL) {
if ($node === NULL) return getElementById($doc, $id, $doc->documentElement);
$children = $node->childNodes;
for ($i = 0; $i < $children->length; ++$i) {
$elem = $children->item($i);
if (!($elem instanceof DOMElement)) continue;
if ($elem->getAttribute('id') == $id) return $elem;
$ret = getElementById($doc, $id, $elem);
if ($ret !== NULL) return $ret;
}
return NULL;
}