为什么正则表达式会匹配否定字符类中列出的重音/unicode 字符?

发布于 2024-09-24 15:26:24 字数 187 浏览 0 评论 0原文

$string1 = preg_replace('/[^A-Za-z0-9äöü!&_=\+-]/', ' ', $string4);

此正则表达式不应替换字符 äöü。 在 Ruby 中,它按预期工作。 但在 PHP 中,它还取代了 ä ö 和 ü。

有人可以给我提示如何修复它吗?

$string1 = preg_replace('/[^A-Za-z0-9äöü!&_=\+-]/', ' ', $string4);

This Regex shouldn't replace the chars äöü.
In Ruby it worked as expected.
But in PHP it replaces also the ä ö and ü.

Can someone give me a hint how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上青苔 2024-10-01 15:26:24

Set the u pattern modifier (to tell php to treat the regex as a UTF-8 string).

'/[^A-Za-z0-9äöü!&_=\+-]/u'
数理化全能战士 2024-10-01 15:26:24

我认为这应该有效:

$string1 = preg_replace('/\[^A-Za-z0-9\pL!&_=\+-]/u', ' ', $string4 );

i think this should work:

$string1 = preg_replace('/\[^A-Za-z0-9\pL!&_=\+-]/u', ' ', $string4 );
得不到的就毁灭 2024-10-01 15:26:24

Unicode 支持是 PHP 6 承诺的功能之一。

目前在 php5

Unicode support is one of the features promised for PHP 6.

Currently in php5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文