Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 如何删除DomeElement的style属性的特定样式属性?_Php_Recursion_Domdocument - Fatal编程技术网

Php 如何删除DomeElement的style属性的特定样式属性?

Php 如何删除DomeElement的style属性的特定样式属性?,php,recursion,domdocument,Php,Recursion,Domdocument,我有一个PHP的DomeElement。我想从元素的style属性及其所有子元素中删除一些特定的样式。我知道它应该是递归的 例如,我有: 我删除了top样式。它转换为: 使用JQUERY尝试下面的代码 $( document ).ready(function() { $('a *').css('top',''); // For Remove specific property of child element $('a').css('top',''); // For Remove speci

我有一个PHP的DomeElement。我想从元素的style属性及其所有子元素中删除一些特定的样式。我知道它应该是递归的

例如,我有:

我删除了
top
样式。它转换为:


使用JQUERY尝试下面的代码

$( document ).ready(function() {
$('a *').css('top',''); // For Remove specific property of child element

$('a').css('top',''); // For Remove specific property of element
});

对父节点使用以下函数:

 private function prevent_html_style_properties($element){
    if($element->nodeType != XML_ELEMENT_NODE)
        return $element;
    $element->removeAttribute('class');
    if($element->hasAttribute('style')){
        $style = $element->getAttribute('style');
        $existingDeleteAttrList = array();
        $attrList = [
            'height',
            'position',
            'top'];
        $style_parts = explode(';', $style);
        foreach($attrList as $attr){
            if(strpos($style, $attr) !== false)
                $existingDeleteAttrList[] = $attr;
        }
        $new_attr_value = '';
        foreach($style_parts as $style_part){
            $attr_is_safe = true;
            foreach($existingDeleteAttrList as $attr){
                if(strpos($style_part, $attr) !== false) {
                    $attr_is_safe = false;
                    break;
                }
            }
            if($attr_is_safe)
                $new_attr_value .= $style_part . ';';
        }
        $element->setAttribute('style', $new_attr_value);
    }

    $children  = $element->childNodes;
    foreach ($children as $child)
    {
        $element->replaceChild($this->prevent_html_tag_styles($child), $child);
    }
    return $element;
}
用法:

 $element = $this->prevent_html_style_properties($element);
一些解释:

  • 有些子项是简单文本,没有任何属性。所以我们必须检查它是否是
    XML\u元素\u节点
  • 它使用
    replaceChild
    修复其直接子级,依此类推

不错。问题出在哪里?你的代码在哪里?