求解關於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