代码:
=============================================
$html=array();
$html['body'] = '<section style="display: inline-block;margin:2px 3px;color: rgb(63, 63, 63);letter-spacing: 1.5px;padding: 6px 1em;box-sizing: border-box;" class="135brush" data-brushtype="text"><img src="images/11.png" width="200" height="300" style="margin:5px 5px;"  /><br/><img src="images/22.png" width="200" height="300" style="margin:5px 5px;" /></section>';
==============================================我想将body里面的html代码中的margin:2px 3px;和margin:5px 5px;都替换成margin:0 0;
将html代码中的所有<img 中含有height=""的替换成空串。这两个正则表达式应当怎么写。求教!
用preg_replace或者其它方法?

解决方案 »

  1.   

    php 函数: 
    // 替换margin
    $html['body'] = str_replace( ''margin:2px 3px;" , "margin:0 0;", $html['body']);//替换img 
    $html['body'] = preg_replace("/^<img \s height="\dpx"/,"",$html['body']);
      

  2.   

    //替换img 
    $html['body'] = preg_replace("/^<img \s height="\dpx"/,"",$html['body']);
    ---------这句语法不对哦。
    而且我只是去掉里面的height="*"的部分,其它不动的。
      

  3.   


    $str=<<<STR
    <section style="display: inline-block;margin:2px 3px;color: rgb(63, 63, 63);letter-spacing: 1.5px;padding: 6px 1em;box-sizing: border-box;" class="135brush" data-brushtype="text"><img src="images/11.png" width="200" height="300" style="margin:5px 5px;"  /><br/><img src="images/22.png" width="200" height="300" style="margin:5px 5px;" /></section>
    STR;$str = preg_replace(array('/margin:.*;/isU', '/(<img.*)height=".*"/isU'), array('margin:0 0;', '$1'), $str);
    echo($str);// output
    // <section style="display: inline-block;margin:0 0;color: rgb(63, 63, 63);letter-spacing: 1.5px;padding: 6px 1em;box-sizing: border-box;" class="135brush" data-brushtype="text"><img src="images/11.png" width="200"  style="margin:0 0;"  /><br/><img src="images/22.png" width="200"  style="margin:0 0;" /></section