Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在foreach循环中删除DOMNode中的所有属性_Php_Dom - Fatal编程技术网

Php 在foreach循环中删除DOMNode中的所有属性

Php 在foreach循环中删除DOMNode中的所有属性,php,dom,Php,Dom,所以这不起作用: foreach ($element->attributes as $attribute) { $element->removeAttribute($attribute->name); } 如果节点有2个属性,则仅删除第一个属性 我尝试克隆域名NodeMap,但没有成功: $attributesCopy = clone $element->attributes; fo

所以这不起作用:

        foreach ($element->attributes as $attribute) {
            $element->removeAttribute($attribute->name);
        }
如果节点有2个属性,则仅删除第一个属性

我尝试克隆域名NodeMap,但没有成功:

        $attributesCopy = clone $element->attributes;
        foreach ($attributesCopy  as $attribute) {
            $element->removeAttribute($attribute->name);
        }
仍然只删除第一个属性

这里解释了这个问题: 显然这是一个特性,而不是一个bug。但是评论中没有提到解决方案。

简单地说:

$attributes = $element->attributes;
while ($attributes->length) {
    $element->removeAttribute($attributes->item(0)->name);
}

由于属性集合会在删除属性后自动重新编制索引,因此只需继续删除属性零,直到没有剩余属性。

尝试回显$attribute->name并检查它是否正在更改?能否向我们提供您正在处理的DOM树?