donglin9068 2018-05-16 06:59
浏览 186
已采纳

PHP preg_match_all不处理大数据

Why is preg_match_all not working?

$pattern = '/\{(?:[^{}]|(?R))*\}/';

$result = 161240 characters

       if (preg_match_all($pattern, $result, $matches)) {
            echo 'Success';
        } else {
            echo 'Not working';
        }

This Displays: "Not working"

  • 写回答

1条回答 默认 最新

  • dongxing2692 2018-05-16 07:50
    关注

    The pattern at play matches balanced curly brackets using regex recursion. The pattern itself looks fine and works as intended.

    <?php
    $re = '/{(?>[^{}]|(?R))*\}/m';
    $str = 'Why is preg_match_all not working?{{{{{
    $pattern = \'/\\{(?:[^{}]|(?R))*\\}/\';
    $result = 161240 characters
           if (preg_match_all($pattern, $result, $matches)){ {
                echo \'Success\';
            } else {
                echo \'Not working\';
            }
    }}}}}}{}{}{}{}{}{}{}{{{{{{}}}}}';
    
    //preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    if (preg_match_all($re, $str, $matches)) {
    echo 'Success
    ';
    } else {
    echo 'Not working
    ';
    }
    
    // Print the entire match result
    var_dump($matches);
    

    This also works with larger input (here tested with ~5000 characters). The most likely explanation is: the pattern does not find a valid match.

    However, you are running a recursive regex on a very large input string. Many, things could go wrong. The PCRE engine reaches internal limits, your string is not properly encoded, timeouts, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?