preg_replace - NULL 结果?

发布于 2024-08-04 12:13:16 字数 313 浏览 6 评论 0原文

这是一个小示例(下载,重命名为 .php 并在 shell 中执行):

test.txt< /a>

为什么 preg_replace 返回 NULL 而不是原始字符串?

\x{2192} 与 HTML“”(“→”)相同。

Here's a small example (download, rename to .php and execute it in your shell):

test.txt

Why does preg_replace return NULL instead of the original string?

\x{2192} is the same as HTML "" ("→").

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

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

发布评论

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

评论(4

青春有你 2024-08-11 12:13:16

当我的正则表达式包含 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.

南渊 2024-08-11 12:13:16

preg_replace() 的文档中:

返回值

preg_replace() 返回一个数组,如果
subject 参数是一个数组,或者
否则为字符串。

如果找到匹配项,则新主题
将被退回,否则以
将原封不动地返回或NULL,如果
发生错误。

在您的模式中,我认为不支持 u 标志。 错误

编辑:主题似乎存在某种编码问题。当我删除“147 3.2 V6 - GTA (184 kW)”并手动重新输入时,一切似乎都正常。

编辑 2:在您提供的模式中,有 3 个空格似乎给正则表达式引擎带来了问题。当我将它们转换为十进制时,它们的值为 160(而不是正常的空格 32)。当我用正常的空间替换这些空间时,它似乎起作用了。

我已用下面的下划线替换了有问题的空格:

'147 3.2 V6 - GTA (184 kW)'
'147 3.2_V6 - GTA_(184_kW)'

From the documentation on preg_replace():

Return Values

preg_replace() returns an array if the
subject parameter is an array, or a
string otherwise.

If matches are found, the new subject
will be returned, otherwise subject
will be returned unchanged or NULL if
an error occurred.

In your pattern, I don't think the u flag is supported. WRONG

Edit: 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:

'147 3.2 V6 - GTA (184 kW)'
'147 3.2_V6 - GTA_(184_kW)'
绿光 2024-08-11 12:13:16
  • 您使用的是单引号,这意味着您唯一可以转义的是其他单引号。要启用转义序列(例如\x32,然后使用双引号“”)
  • 我不是UTF8专家,但转义码\x2192也不正确。您可以执行以下操作: \x21\x92 将两个字节都放入字符串中,但您可能需要查看 utf8_encodeutf8_decode
  • 您的源字符串其中包含无效字符或其他内容。 PHP 给出:
    警告:preg_replace():编译失败:test.php 第 7 行中偏移量 0 处的 UTF-8 字符串无效
  • You are using single quotes, which means the only thing that you can escape is other single quotes. To enable escape sequences (e.g. \x32, then use double quotes "")
  • I am not a UTF8 expert, but the escape code \x2192 is not correct either. You can do: \x21\x92 to get both bytes into your string, but you may want to look at utf8_encode and utf8_decode
  • Your source string has invalid characters in it, or something. PHP gives:
    Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in test.php on line 7
梅窗月明清似水 2024-08-11 12:13:16

我相信您的正则表达式也存在错误:~\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

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