preg_replace 使用 unicode

preg_replace with unicode

我使用以下带 preg_replace 的正则表达式来去除字符串中的任何标点符号:

$string = preg_replace("#((?!-|')\pP)+#", '', $string);

但我意识到它破坏了一些 unicode 字符。当字符串是这样的 "höpöttää?!..." 时,我得到这个 "h�p�ttää",没有标点符号,但字符被破坏了。

我阅读了 PHP 文档并找到了一些使用 `...`u 修饰符的建议。所以我尝试了这个:

$string = preg_replace("`#((?!-|')\pP)+#`u", '', $string);

它确实解决了字符问题。但现在它停止删除标点符号。使用此字符串 "höpöttää?!...",我得到相同的 "höpöttää?!...".

不知道反引号在那里做什么。

$string = preg_replace("#(?![-'])\pP#u", '', $string);

$string = preg_replace("#[^-'\PP]#u", '', $string);

DEMO