Reputation: 83695
So this doesn't work:
foreach ($element->attributes as $attribute) {
$element->removeAttribute($attribute->name);
}
If the node has 2 attributes, it only removes the first one.
I tried cloning the DOMNamedNodeMap with no success:
$attributesCopy = clone $element->attributes;
foreach ($attributesCopy as $attribute) {
$element->removeAttribute($attribute->name);
}
Still removes only first attribute.
This issue is explained here: http://php.net/manual/en/class.domnamednodemap.php Apparently it is a feature, not a bug. But there is no solution mentioned in the comments.
Upvotes: 5
Views: 3923
Reputation: 437376
Simply:
$attributes = $element->attributes;
while ($attributes->length) {
$element->removeAttribute($attributes->item(0)->name);
}
Since the attributes collection automatically reindexes as soon as an attribute is removed, just keep on removing attribute zero until none are left.
Upvotes: 14