如何使用 PHP ereg_replace replace 替换所有特定字符?

How replace replace all specific characters using PHP ereg_replace?

我想在 PHP 上使用正则表达式将特定符号替换为符号 %。例如。

$result = ereg_replace('xlp','%','example')`. //$result = `'e%a%%me'

这是可能的还是我应该使用其他方式?

首先,关于ereg_replace

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

改用preg_replace

接下来,在您的模式中,您将搜索文字字符串 xlp。将它们放在一个字符集中以匹配 三者之一。

$result = preg_replace(
    "/[xlp]/",
    "%",
    $string
);

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