我希望在PHP上使用正则表达式替换符号%的特定符号。例如。
$result = ereg_replace('xlp','%','example')`. //$result = `'e%a%%me'
有可能或我应该用其他方式吗?
答案 0 :(得分:3)
首先,关于ereg_replace
:
自PHP 5.3.0起,此功能已被弃用。依靠这个 功能非常沮丧。
改为使用preg_replace
。
接下来,在您的模式中,您正在搜索文字字符串xlp
。将它们放在一个字符集中以匹配 三个
$result = preg_replace(
"/[xlp]/",
"%",
$string
);
答案 1 :(得分:3)
preg_replace
很好,但我们不要忘记str_replace
print str_replace(array('x','l','p'),array('%','%','%'),'example');
// or
print str_replace(array('x','l','p'),'%','example');
//will output
e%am%%e