php 正则表达式 utf-8 中的单词边界匹配
我在 utf-8 php 文件中有以下 php 代码:
var_dump(setlocale(LC_CTYPE, 'de_DE.utf8', 'German_Germany.utf-8', 'de_DE', 'german'));
var_dump(mb_internal_encoding());
var_dump(mb_internal_encoding('utf-8'));
var_dump(mb_internal_encoding());
var_dump(mb_regex_encoding());
var_dump(mb_regex_encoding('utf-8'));
var_dump(mb_regex_encoding());
var_dump(preg_replace('/\bweiß\b/iu', 'weiss', 'weißbier'));
我希望最后一个正则表达式仅替换完整单词而不是部分单词。
在我的 Windows 计算机上,它返回:
string 'German_Germany.1252' (length=19)
string 'ISO-8859-1' (length=10)
boolean true
string 'UTF-8' (length=5)
string 'EUC-JP' (length=6)
boolean true
string 'UTF-8' (length=5)
string 'weißbier' (length=9)
在网络服务器(Linux)上,我得到:
string(10) "de_DE.utf8"
string(10) "ISO-8859-1"
bool(true)
string(5) "UTF-8"
string(10) "ISO-8859-1"
bool(true)
string(5) "UTF-8"
string(9) "weissbier"
因此,正则表达式在 Windows 上按我的预期工作,但在 Linux 上却不行。
所以主要问题是,我应该如何编写正则表达式以仅在单词边界匹配?
第二个问题是我如何让 Windows 知道我想在我的 php 应用程序中使用 utf-8。
I have the following php code in a utf-8 php file:
var_dump(setlocale(LC_CTYPE, 'de_DE.utf8', 'German_Germany.utf-8', 'de_DE', 'german'));
var_dump(mb_internal_encoding());
var_dump(mb_internal_encoding('utf-8'));
var_dump(mb_internal_encoding());
var_dump(mb_regex_encoding());
var_dump(mb_regex_encoding('utf-8'));
var_dump(mb_regex_encoding());
var_dump(preg_replace('/\bweiß\b/iu', 'weiss', 'weißbier'));
I would like the last regex to replace only full words and not parts of words.
On my windows computer, it returns:
string 'German_Germany.1252' (length=19)
string 'ISO-8859-1' (length=10)
boolean true
string 'UTF-8' (length=5)
string 'EUC-JP' (length=6)
boolean true
string 'UTF-8' (length=5)
string 'weißbier' (length=9)
On the webserver (linux), I get:
string(10) "de_DE.utf8"
string(10) "ISO-8859-1"
bool(true)
string(5) "UTF-8"
string(10) "ISO-8859-1"
bool(true)
string(5) "UTF-8"
string(9) "weissbier"
Thus, the regex works as I expected on windows but not on linux.
So the main question is, how should I write my regex to only match at word boundaries?
A secondary questions is how I can let windows know that I want to use utf-8 in my php application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使在 UTF-8 模式下,像
\w
和\b
这样的标准类简写也不能识别 Unicode。正如您所解决的,您只需使用 Unicode 简写,但您可以通过使用环视而不是交替来使其不那么难看:另请注意我如何将花括号从 Unicode 类简写中删除;当类名由单个字母组成时,您可以这样做。
Even in UTF-8 mode, standard class shorthands like
\w
and\b
are not Unicode-aware. You just have to use the Unicode shorthands, as you worked out, but you can make it a little less ugly by using lookarounds instead of alternations:Notice also how I left the curly braces out of the Unicode class shorthands; you can do that when the class name consists of a single letter.
猜测这与 Bug #52971 有关
以及在 PHP 5.3.4 中已修复
Guess this was related to Bug #52971
and fixed in PHP 5.3.4
这是我到目前为止所发现的。通过像这样重写搜索和替换模式:
我得到了想要的结果:
在运行 apache 的 Windows 计算机上和运行 apache 的托管 Linux Web 服务器上。
我认为有一些更好的方法可以做到这一点。
另外,我仍然想将我的 Windows 计算机的语言环境设置为 utf-8。
here is what I have found so far. By rewriting the search and replacement patterns like this:
I get the wanted result:
on both my windows computer running apache and on the hosted linux webserver running apache.
I assume there is some better way to do this.
Also, I still would like to setlocale my windows computer to utf-8.
根据 此评论,这是一个错误PHP。使用
\W
代替\b
有什么好处吗?According to this comment, that is a bug in PHP. Does using
\W
instead of\b
give any benefit?