preg_replace_callback出现内存泄露。有人解决了么?
环境:php5.4.22 +centos 5.4+nginx
--------------------------------------------------------------------------------------------------------------
下面这个问题发现的:http://bbs.csdn.net/topics/390784375
----------------------------------
同样问题:
http://bbs.csdn.net/topics/390693060
----------------------------------------------------------------------
测试代码:
$content='asdfsadfasdfsadfasdfsadfasdfsadfasdfsadfasdfsadf testtest';
$a=123;
$content= preg_replace_callback('/(.*?)<\/php(\s*?)>/is', function($match) use($a){return 123;}, $content);
echo $content;
----------------------------------------------------运行结果-----------------------
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3086503041 bytes) in /data/web/partTime/test2.php on line 4
---------------------------------------------
------解决思路----------------------如果是:
先 preg_match_all
再 preg_replace
也会出问题吗?
------解决思路----------------------既然 preg_replace_callback 会有内存泄露
那么应该在任何文件中都是这样的
你可以单独用一个文件测试一下
$content = preg_replace_callback('/[a-z]/', function ( $matches ) {
return strtoupper($matches[0]);
}, 'abdfrew');
echo $content;
与之等价的分立代码为
$content = 'abdfrew';
preg_match_all('/[a-z]/', $content, $matches );
foreach($matches[0] as $v) $r[$v] = strtoupper($v);
$content = strtr($content, $r);
echo $content;
先分别运行一下,看有无问题