V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
dsphper
V2EX  ›  问与答

PHP 正则引擎 bug,通杀 PHP 所有版本。

  •  
  •   dsphper · Aug 11, 2020 · 2379 views
    This topic created in 2085 days ago, the information mentioned may be changed or developed.
    
    $re = '/@([^@\s::]+)[:| |:]+/m';
    $str = '@多阿斯顿撒 2111 撒奥:asdsada @asdsadsadsad 阿斯顿撒大:@阿斯顿撒大所:@阿斯顿撒多撒多撒 sad 撒大撒多';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    // Print the entire match result
    var_dump($matches);
    
    

    测试了下所有版本的 php 解析的结果都是错误的,其他语言 Python,Js 等可以正常解析。

    错误的结果

    aLNbex.png

    正确的解析结果

    aLUSld.png

    9 replies    2020-08-11 19:57:04 +08:00
    gz911122
        1
    gz911122  
       Aug 11, 2020
    截图的那个正则网站是啥, 看起来不错啊
    dsphper
        2
    dsphper  
    OP
       Aug 11, 2020
    @gz911122 regex101
    CismonX
        3
    CismonX  
       Aug 11, 2020   ❤️ 5
    这个是引擎不识别 unicode 字符的问题。改成:

    $re = '/(*UTF8)@([^@\s::]+)[:| |:]+/m';

    就没问题了。
    iyaozhen
        4
    iyaozhen  
       Aug 11, 2020   ❤️ 1
    不要见风就是雨

    /u
    才能识别中文
    vc1
        5
    vc1  
       Aug 11, 2020
    执行 php 代码的图是哪个网站
    dsphper
        6
    dsphper  
    OP
       Aug 11, 2020
    @iyaozhen
    以感谢老哥
    dsphper
        7
    dsphper  
    OP
       Aug 11, 2020
    @CismonX 4 楼老哥解决方法简单粗暴。
    dsphper
        8
    dsphper  
    OP
       Aug 11, 2020
    @vc1 3v4l
    azh7138m
        9
    azh7138m  
       Aug 11, 2020 via iPhone
    @iyaozhen 不要见着风是得雨

    你们这样子不行的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2994 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 15:22 · PVG 23:22 · LAX 08:22 · JFK 11:22
    ♥ Do have faith in what you're doing.