PHP test function preg_match

    If the PHP function preg_match passes a string consisting of 100,000 (one hundred thousand) or more syllables as the source string, then it will return an incorrect result. More precisely, the result in this case will always be false.

    It is checked very simply. The result of this script will be such a message. This suggests that preg_match has not mastered a string with a length of one hundred thousand characters. With longer lines, the result will be the same. Although it’s not completely clear to me why the function stumbles on a string that is 100,000 bytes long. Apparently this is a feature of the implementation. Checked for PHP Version 5.2.6-1. Therefore, I recommend that you refrain from using the preg_match function when it may have to process large strings.
    $max_n=100010;
    $n=99900;
    $tmp_str=str_repeat('b',$n);
    $go=true;
    while (($go)&&($n<=$max_n)) {
    $n++;
    $tmp_str.='b';
    $str='a'.$tmp_str.'a';
    $go=preg_match('/a.*?a/sim', $str);
    if (!$go) {
    echo 'error on length '.strlen($str);
    die;
    }
    }
    echo ' ok';


    error on length 100000







    All of the above is also true for the PHP function preg_match_all.

    UPD Solving the problem from the comment l2k
    ini_set(«pcre.backtrack_limit»,10000000);
    Thus, the limit of PCRE backlinks can be changed.
    Moreover, the mutability of the directive PHP_INI_ALL, which means that it can be installed anywhere.

    Also popular now: