Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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
删除php5 dom中循环中仅执行一次的AttributeNode_Php_Dom_Attributes_Anchor_Href - Fatal编程技术网

删除php5 dom中循环中仅执行一次的AttributeNode

删除php5 dom中循环中仅执行一次的AttributeNode,php,dom,attributes,anchor,href,Php,Dom,Attributes,Anchor,Href,我正在搜索删除锚标记中除href以外的所有属性的脚本。我发现了一些脚本,它们都与我编写的解决方案非常相似 $okattrs = array( 'a' => array('href' => true), ); $content = '<div id="att"><a class="pl" title="hit me" id="myid" href="http://google.com" >Click On Goo

我正在搜索删除锚标记中除href以外的所有属性的脚本。我发现了一些脚本,它们都与我编写的解决方案非常相似

$okattrs = array(
            'a' => array('href' => true),
            );

$content = '<div id="att"><a class="pl" title="hit me" id="myid" href="http://google.com" >Click On Google</a></div>';
$doc = new DomDocument();
$doc->loadHTML($content);

$a_tags = $doc->getElementsByTagName('a');
foreach($a_tags as $k => $a_tag_obj) {
    foreach ($a_tag_obj->attributes as $name => $attrNode)
    {
        if (!isset($okattrs[$a_tag_obj->nodeName][$name]))
        {
            $a_tag_obj->removeAttributeNode($attrNode);
        }
    }
}
echo    $content1=$doc->saveHTML();
$okattrs=数组(
'a'=>array('href'=>true),
);
$content='';
$doc=新的DomDocument();
$doc->loadHTML($content);
$a_tags=$doc->getElementsByTagName('a');
foreach($k=>$a\u标记\u对象){
foreach($a_tag_obj->attributes as$name=>$attrNode)
{
如果(!isset($okattrs[$a_tag_obj->nodeName][$name]))
{
$a_tag_obj->removeAttributeNode($attrNode);
}
}
}
echo$content1=$doc->saveHTML();
现在我的问题是,它只删除第一个属性,即“class”。我调试并发现,若我删除具有removeAttributeNode循环的行,它可以正常工作4次,但若使用这个循环只工作一次,那个么只删除第一个属性并中断


以前有人遇到过这个问题或有解决方案吗?

最后我让代码运行起来。只是一个将removeAttributeNode移动到另一个循环的更改

foreach($a_tags as $a_tag_obj) { 
    $attribute_list = array(); 
    foreach ($a_tag_obj->attributes as $name => $attrNode) { 
        if (!isset($okattrs[$a_tag_obj->nodeName][$name])) { 
            $attribute_list[] = $name; 
        }    
    } 
    for($i=0;$i<sizeof($attribute_list);$i++) { 
        $a_tag_obj->removeAttribute($attribute_list[$i]); 
    }
}
foreach($a_tags as$a_tag_obj){
$attribute_list=array();
foreach($a_tag_obj->attributes as$name=>$attrNode){
如果(!isset($okattrs[$a_tag_obj->nodeName][$name]){
$attribute_list[]=$name;
}    
} 
对于($i=0;$iremoveAttribute($attribute_list[$i]);
}
}