PHP求一个正则表达式,有条件替换,实现效果如下,
style="color:#ffffff;font-size:12x;" 替换成空
style="display:none;" 替换不变
style="font-size:12x;display:none;color:#ffffff;" 替换成 style="display:none;"也就是在替换style="******"里面,要是有display:none;的就是替换成style="display:none;",没有的就替换成空字符串。谢谢

解决方案 »

  1.   

    str_replace(array('color:#ffffff;','font-size:12x;'),'',$str)
      

  2.   

    不是这样,我的意思是要替换所有网页里面的style="******",除非里面有display:none;不然就替换为空
      

  3.   


    $str = <<<STR
    <l style="asdf"/><b style="asss;display:none;fds" /><c style="tttt" />
    STR;
    echo htmlspecialchars(preg_replace_callback('/style="(.+)"/iU','test', $str));
    function test($matches)
    {
        if(preg_match('/display:none;/i', $matches[1])) {
            return 'style="display:none;"';
        }
        return 'style=""';
    }
    /*输出结果:
    <l style=""/><b style="display:none;" /><c style="" />
    */
      

  4.   


    preg_replace(array('#style="[^"]*(display:none;)[^"]*"#iU','#style="[^"]*"#iU'),array('style="\\1"','style=""',),$html);