Regular expression greedy max string?

Hi~ All this is my Tesing code

<?php
$html1 = '<table>';
$html2 = '01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234';
$html3 = '</table>';

for ($i = 40; $i <= 50; $i++) {
    $html = $html1;
    for ($j = 0; $j <= $i; $j++) {
        $html.= $html2;
    }
    $html.= $html3;
    if (preg_match('/<table.+?<\\/table>/is', $html)) {
        echo strlen($html) . "=>success<br>";
    } else {
        echo strlen($html) . "=>fail<br>";
    }
}

Result:
85090=>success
87165=>success
89240=>success
91315=>success
93390=>success
95465=>success
97540=>success
99615=>success
101690=>fail
103765=>fail
105840=>fail

Is the max string is 100012?

It looks to me like you’re hitting the backtrack limit (defaults to 100,000). Check for yourself by seeing if the error code returned from [url=http://php.net/preg_last_error]preg_last_error() is PREG_BACKTRACK_LIMIT_ERROR.

How to alleviate the problem is up to you, raise the limit or change the way that the code works so that it does not backtrack beyond the limit.

Thank you!:slight_smile:

find php.ini pcre.backtrack_limit=1000000