1
$str = str_repeat('a', 1024 * 1024);
//$str = str_repeat('a', 1024);
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);

echo strlen($temp);

With str_repeat('a', 1024) the length of $temp is 1024 but with str_repeat('a', 1024 * 1024) the length of $temp is 0.

I'm running PHP 7.4.3.

What could be the problem?

Dharman
  • 30,962
  • 25
  • 85
  • 135
neubert
  • 15,947
  • 24
  • 120
  • 212
  • 2
    Should see https://www.php.net/manual/en/function.preg-last-error.php. – user3783243 Jul 16 '20 at 13:09
  • _With str_repeat('a', 1024) the length of $temp is 1024 but with str_repeat('a', 1024 * 1024) the length of $temp is 0_ not really. https://3v4l.org/1b4SE – nice_dev Jul 16 '20 at 13:21
  • It has obviously something to do with preg_replace. – nice_dev Jul 16 '20 at 13:23
  • *looks* like you're hitting a [PCRE backtrack limit](https://www.php.net/manual/en/pcre.configuration.php#ini.pcre.backtrack-limit): https://3v4l.org/02cQ8 – CD001 Jul 16 '20 at 13:24

2 Answers2

1

You actually got an error

Process exited with code 137

on this function:

$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);

and that ends up the $str to be NULL and automatically your strlen($temp); is actually strlen(NULL); which gives you 0.


1024 * 1024 = 1048576

and the default setting in php.ini for pcre.backtrack_limit is 1000000

To fix that problem change this setting inside php.ni file

pcre.backtrack_limit=1048577
lewis4u
  • 14,256
  • 18
  • 107
  • 148
1

It is an with char limit set in php.ini to handle regular expressions. By default it will be 100000. The given string length 1024 * 1024 = 1048576 exceeds the limit. change

pcre.backtrack_limit=1048577

in php.ini and restart the apache it will work