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.

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

报告相同问题?

悬赏问题

  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送