V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
dsphper
V2EX  ›  问与答

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

  •  
  •   dsphper · 2020-08-11 12:29:35 +08:00 · 1936 次点击
    这是一个创建于 1325 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    $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 条回复    2020-08-11 19:57:04 +08:00
    gz911122
        1
    gz911122  
       2020-08-11 13:20:47 +08:00
    截图的那个正则网站是啥, 看起来不错啊
    dsphper
        2
    dsphper  
    OP
       2020-08-11 13:25:09 +08:00
    @gz911122 regex101
    CismonX
        3
    CismonX  
       2020-08-11 13:59:11 +08:00   ❤️ 5
    这个是引擎不识别 unicode 字符的问题。改成:

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

    就没问题了。
    iyaozhen
        4
    iyaozhen  
       2020-08-11 14:45:25 +08:00   ❤️ 1
    不要见风就是雨

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

    你们这样子不行的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5108 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.