试图理解 preg_replace 在大字符串上的行为
trying to understand preg_replace's behavior on large strings
$str = str_repeat('a', 1024 * 1024);
//$str = str_repeat('a', 1024);
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
echo strlen($temp);
对于str_repeat('a', 1024)
,$temp
的长度是1024,对于str_repeat('a', 1024 * 1024)
,$temp
的长度是0。
我是 运行 PHP 7.4.3.
可能是什么问题?
你实际上遇到了一个错误
Process exited with code 137
关于这个函数:
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
最终 $str 为 NULL
并且你的 strlen($temp);
实际上是 strlen(NULL);
自动给你 0
.
1024 * 1024 = 1048576
php.ini 中 pcre.backtrack_limit 的默认设置为 1000000
要解决该问题,请在 php.ni 文件
中更改此设置
pcre.backtrack_limit=1048577
这是一个在php.ini中设置的字符限制来处理正则表达式。默认情况下,它将是 100000。给定的字符串长度 1024 * 1024 = 1048576 超出限制。变化
pcre.backtrack_limit=1048577
在 php.ini 中并重新启动 apache 它将工作
$str = str_repeat('a', 1024 * 1024);
//$str = str_repeat('a', 1024);
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
echo strlen($temp);
对于str_repeat('a', 1024)
,$temp
的长度是1024,对于str_repeat('a', 1024 * 1024)
,$temp
的长度是0。
我是 运行 PHP 7.4.3.
可能是什么问题?
你实际上遇到了一个错误
Process exited with code 137
关于这个函数:
$temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
最终 $str 为 NULL
并且你的 strlen($temp);
实际上是 strlen(NULL);
自动给你 0
.
1024 * 1024 = 1048576
php.ini 中 pcre.backtrack_limit 的默认设置为 1000000
要解决该问题,请在 php.ni 文件
中更改此设置pcre.backtrack_limit=1048577
这是一个在php.ini中设置的字符限制来处理正则表达式。默认情况下,它将是 100000。给定的字符串长度 1024 * 1024 = 1048576 超出限制。变化
pcre.backtrack_limit=1048577
在 php.ini 中并重新启动 apache 它将工作