preg_replace - NULL 结果?
这是一个小示例(下载,重命名为 .php 并在 shell 中执行):
为什么 preg_replace
返回 NULL 而不是原始字符串?
\x{2192}
与 HTML“→
”(“→”)相同。
Here's a small example (download, rename to .php and execute it in your shell):
Why does preg_replace
return NULL instead of the original string?
\x{2192}
is the same as HTML "→
" ("→").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我的正则表达式包含
u
UTF-8 PCRE 修饰符。如果您的源文本不是 UTF,并且您有此修饰符,您将得到 null 结果。I had an null response when my regular expression included the
u
UTF-8 PCRE modifier. If your source text is not UTF and you have this modifier, you'll get a null result.从 preg_replace() 的文档中:
在您的模式中,我认为不支持 u 标志。错误编辑:主题似乎存在某种编码问题。当我删除“147 3.2 V6 - GTA (184 kW)”并手动重新输入时,一切似乎都正常。
编辑 2:在您提供的模式中,有 3 个空格似乎给正则表达式引擎带来了问题。当我将它们转换为十进制时,它们的值为 160(而不是正常的空格 32)。当我用正常的空间替换这些空间时,它似乎起作用了。
我已用下面的下划线替换了有问题的空格:
From the documentation on preg_replace():
In your pattern, I don't think the u flag is supported.WRONGEdit: It seems like some kind of encoding issue with the subject. When I erase '147 3.2 V6 - GTA (184 kW)' and manually re-type it everything seems to work.
Edit 2: In the pattern you provided, there are 3 spaces that seem to be giving issues to the regex engine. When I convert them to decimal their value is 160 (as opposed to normal space 32). When I replace those spaces with normal ones it seems to work.
I've replaced the offending spaces with underscores below:
\x32
,然后使用双引号“”)\x2192
也不正确。您可以执行以下操作:\x21\x92
将两个字节都放入字符串中,但您可能需要查看utf8_encode
和utf8_decode
警告:preg_replace():编译失败:test.php 第 7 行中偏移量 0 处的 UTF-8 字符串无效
\x32
, then use double quotes "")\x2192
is not correct either. You can do:\x21\x92
to get both bytes into your string, but you may want to look atutf8_encode
andutf8_decode
Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in test.php on line 7
我相信您的正则表达式也存在错误:
~\x{2192}~u
尝试替换我的内容,看看是否适合您:
/\x{2192} /u
I believe there is also a fault in your Regex expression:
~\x{2192}~u
Try replacing what I have and see if that works out for you:
/\x{2192}/u