📜 ⬆️ ⬇️

PHP test function preg_match

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

It is checked very simply.
$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';

The result of this script will be such a message.
error on length 100000

That says that preg_match did not master a line long hundred thousand characters. With longer lines, the result will be the same. Although I don’t fully understand why the function stumbles exactly on a line 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 to refrain from using the preg_match function when it may have to handle large strings.

All 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);
This way you can change the PCRE backlinks limit.
And the variability of the directive PHP_INI_ALL, which means that it can be installed anywhere.

Source: https://habr.com/ru/post/58560/


All Articles