奇怪的多字节 preg_replace 问题。它正在将我的数据更改为笑脸!
在 Windows 上使用 PHP 5.3.1。
我只是想在数字和字母之间添加空格,但 PHP 正在破坏我的数据!
$text = "TUES:8:30AM-5:00PMTHURS:8:30AM-5:00PMSAT:8:00AM-1:00PM";
echo preg_replace("/([0-9]+)([A-Z]+)/","\1 \2",$text);
> TUES:8:☺ ☻AM-5:☺ ☻PMTHURS:8:☺ ☻AM-5:☺ ☻PMSAT:8:☺ ☻AM-1:☺ ☻PM
我的文件类型为 ANSI,不,源代码中没有 unicode。
这里有什么好玩的?
Using PHP 5.3.1 on windows.
I am just trying to add spaces between numbers and letters, but PHP is mangling my data!
$text = "TUES:8:30AM-5:00PMTHURS:8:30AM-5:00PMSAT:8:00AM-1:00PM";
echo preg_replace("/([0-9]+)([A-Z]+)/","\1 \2",$text);
> TUES:8:☺ ☻AM-5:☺ ☻PMTHURS:8:☺ ☻AM-5:☺ ☻PMSAT:8:☺ ☻AM-1:☺ ☻PM
My file type ANSI, no there is no unicode in the source.
What the fun is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
$
是你的反向引用指示符,而不是 '\':我打赌
\1
会被翻译成一些时髦的东西......注意奇怪的字符不会改变分钟输入为“30”和“00”之间php 手册说你应该双重转义你的反向引用,或者使用
$
(如果您使用的是 4.04 或更高版本)try using
$
are your backreference indicator, not '\':I'm betting
\1
is getting translated to something funky... notice the strange characters don't change between the minutes input being '30' and '00'the php manual says you should double-escape your backreference, or use
$
(if you are using a version 4.04 or newer)在用双引号分隔的字符串中使用它们时,应该使用双反斜杠:
You should use double backslash when you using them in string separated by double quotes:
\1
和\2
被 PHP 转义,并被解释为 ASCII 代码 1 和 2,在大多数标准 Windows 字体中显示为两个笑脸看到了(当我在 Linux 机器上运行相同的程序时,我得到字符代码符号 0001 和 0002,而不是笑脸)。如果您想实际使用正则表达式替换符号,则需要执行以下两件事之一:
为正则表达式字符串使用单引号,以便 PHP 不会将斜杠用作转义字符:
使用双引号,但转义斜杠:
我建议使用单引号解决方案,因为它更易于阅读。
请注意,使用双引号时,PHP 转义将始终优先于正则表达式转义。这可能会影响您的正则表达式模式和替换字符串。无论如何,许多 PHP 转义字符对于正则表达式来说都是相同的 - 例如,
\n
在正则表达式模式中的工作方式相同,无论它是由 PHP 还是由正则表达式转义。但有些功能的工作原理与您发现的不同,因此您需要小心。The
\1
and\2
are being escaped by PHP, and being interpreted as ASCII codes 1 and 2, which in most standard Windows fonts show up as the two smiley faces you're seeing (when I run the same program on my Linux box, I get character code symbols 0001 and 0002 instead of the smiley faces).If you want to actually use the regex replacement symbols, you need to do one of two things:
Use single quotes for your regex strings, so that the slashes aren't used as escaping characters by PHP:
Use double-quotes, but escape the slashes:
I'd suggest the single quote solution as it's easier to read.
Be aware that with double quotes, PHP escaping will always take precedence over regex escaping. This can affect both your regex pattern and the replacement strings. Many PHP escaped characters are the same for regex anyway - for example,
\n
will work the same in the regex pattern regardless of whether it is escaped by PHP or by regex. But there are some which do not work the same - as you've discovered - so you need to be careful.