preg_replace 用奇怪的字符替换引号?

preg_replace replaces quotes with strange characters?

如果我执行下面的代码:

$string =  'This is “Bob” Batman.';
echo $string . '<br>';
$string = preg_replace('/(["“”„]Bob["“„])/',  '--', $string);
echo $string;

那么结果就是

This is “Bob” Batman.

This is �-�Bob�-�� Batman.

为什么最后一行不是这么简单:

This is -“Bob”- Batman.

这是格式问题。

您可以使用正则表达式 u 标志来解决您的问题:

$string = preg_replace('/(["“”„]Bob["“”„])/u',  '--', $string);

您可以在 unicode 模式下使用此正则表达式:

echo preg_replace('/["“”„]Bob["“”„]/u',  '-[=10=]-', $string);
//=> This is -“Bob”- Batman.

RegEx Demo