从html标签中删除所有内联css,唯一的例外是不删除display:none属性


remove all inline css from html tags only exception is not to remove display:none propety

希望从内联css中删除所有css规则,但不删除display none属性

例如

 <p style="color:red;display:none;float:left;">

我尝试过这个,我使用的是php简单的HTML DOM解析器

摘录在这里

 foreach ($obj->find('img') as $e) {
        $imgSrc = $e -> src;
        preg_match_all('~' . SITE_NAME . '~is', $imgSrc, $match);
        if (count($match[0]) == 0) {
            $loadedSrcs = SITE_NAME . $imgSrc;
        } else {
            $loadedSrcs = $imgSrc;
        }
        $currentImageSrc = $e -> outertext;
        $replacementImageSrc = "src='" . $loadedSrcs . "'";
        $changeSrc = preg_replace('/src=[''|'"][^''|'"]*[''|'"]/is', $replacementImageSrc, $currentImageSrc);
        //$imageSrc = "<img src=".$changeSrc." />";
       $ImagesWithFullPath = preg_replace('~<img [^c]*c=[''|'"]'.$e -> src.'[^'>]*'>~is', $changeSrc , $ImagesWithFullPath);
    // now I want to remove diffrent style property from Image and wandering how could I write the preg_replace not display:none 
}

希望它能帮助理解我的查询

我想用preg_replace(php)而不是Jquery来实现它。

如有任何帮助,我们将不胜感激。

使用HTML解析器获得正确的元素后:

$styles = array_map('trim', explode(';', $e->style));
if (in_array('display:none', $styles)) {
    $e->style='display:none';
}
else {
    $e->style='';
}