dpsyssiv90846 2014-09-22 22:43
浏览 462
已采纳

双转义十六进制字符,即\\ x80 - \\ xFF

I have finally started to understand the context behind escaping hexadecimal characters such as \x80. The documentation talks about the escape sequences, but I can also see that some regular expression use double backslashes such as \\x80 - \\xFF.

What's the difference between \\x80 - \\xFF and \x80 - \xFF when using something like preg_replace ?

  • 写回答

2条回答 默认 最新

  • doutiaoku4495 2014-09-22 23:12
    关注

    When using preg_ functions, your string is parsed twice - first, by php compiler, and then by the PCRE engine. So if you have, for example:

    preg_match("/\x80/"....)
    

    the compiler turns it into

    preg_match("/�/"....) // let � be chr(80)
    

    and passes this to PCRE. When you have two slashes:

    preg_match("/\\x80/"....)
    

    the compiler turns the string into

    preg_match("/\x80/"....)
    

    and then it's the PCRE engine that converts this to the literal character .

    It doesn't make a difference in this particular case, but consider:

    preg_match("/\x5B/"....)
    

    after compilation

    preg_match("/[/"....)
    

    and PCRE fails, because of the dangling metacharacter [. Now if you escape the slash

    preg_match("/\\x5B/"....)
    

    it's compiled to

    preg_match("/\x5B/"....)
    

    which makes PCRE happy, because it understands that [ should be taken literally.

    How exactly php compiles your string depends on the quotes you use: double/single/heredocs/nowdocs. See docs for details. A simple rule of thumb is to use single quotes when possible, if you have to use doubles (for variable interpolation), escape everything twice, even if there's technically no need (e.g "\\b$word\\b").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部