AI编程助手
AI免费问答

求解关于preg_replace函数

  2016-06-23 14:05   1032浏览 原创

我想使用preg_replace函数把text里面的部分字符替换成图片
当我这样写时可以正常执行

$expression ="/@呲牙/";$reexpression ="呲牙";$text=preg_replace($expression,'@@##@@ ',$text);

可是当我改成数组后替换后图片的url都成了www.test.com/static/expression/Array.gif了
$expression =array("/@呲牙/","/@鄙视/","/@折磨/");$reexpression =array("呲牙","鄙视","折磨");$text=preg_replace($expression,'@@##@@ ',$text);

我刚接触php,希望大家能帮忙看一下


回复讨论(解决方案)

在执行函数前会先计算出参数的值。因此也就是:
$replace = ' ';
$text = preg_replace($expression, $replace, $text);
// 由于 $reexpression 是个数组,因此数组和字符串相连就会是Array.gif

看你的需求,重新写一下:

$text = '别@呲牙了,->@鄙视  <br> 另外你的图片以中文命名,还需要注意可能在IE下产生的编码问题,导致图片加载404错误  <br><br> 附手册:   http://php.net/preg-replace  <p class="sougouAnswer">  在执行函数前会先计算出参数的值。因此也就是:  <br> $replace = '<img> ';  <br> $text = preg_replace($expression, $replace, $text);  <br> // 由于 $reexpression 是个数组,因此……  <br> 非常感谢,终于好了 </p><img><img><img>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。