求解關於preg_replace函數解決思路

來源:互聯網
上載者:User
求解關於preg_replace函數
我想使用preg_replace函數把text裡面的部分字元替換成圖片
當我這樣寫時可以正常執行

$expression ="[email protected]/";
$reexpression ="呲牙";
$text=preg_replace($expression,' ',$text);

可是當我改成數組後替換後圖片的url都成了www.test.com/static/expression/Array.gif了

$expression =array("[email protected]/","[email protected]/","[email protected]/");
$reexpression =array("呲牙","鄙視","折磨");
$text=preg_replace($expression,' ',$text);

我剛接觸php,希望大家能幫忙看一下


------解決方案--------------------
在執行函數前會先計算出參數的值。因此也就是:
$replace = ' ';
$text = preg_replace($expression, $replace, $text);
// 由於 $reexpression 是個數組,因此數組和字串相連就會是Array.gif

看你的需求,重新寫一下:
$text = [email protected],->@鄙視<-';
$reexpression =array("呲牙","鄙視","折磨");
// 希望你的程式最好以UTF-8編碼,u修飾符的作用就是避免編碼出現意外的混亂
$find = '#@('. join('
------解決方案--------------------
', $reexpression) .')#u';
$replace = '';
$text = preg_replace($find, $replace, $text);

另外你的圖片以中文命名,還需要注意可能在IE下產生的編碼問題,導致圖片載入404錯誤

附手冊: http://php.net/preg-replace
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.