preg_replace_callback出现内存泄露。有人解决了么?
环境:php5.4.22 +centos 5.4+nginx
————————————————————————————————————–
下面这个问题发现的:http://bbs.gaodaima.com/topics/390784375
———————————-
同样问题:
http://bbs.gaodaima.com/topics/390693060
———————————————————————-
测试代码:
<br /><?php<br />$content='asdfsadfasdfsadfasdfsadfasdfsadfasdfsadfasdfsadf testtest';<br />$a=123;<br />$content= preg_replace_callback('/(.*?)<\/php(\s*?)>/is', function($match) use($a){return 123;}, $content);<br />echo $content;<br />
—————————————————-运行结果———————–
Fatal error: Allowed m@本文9来源gao($daima.com搞@代@#码8网^搞代gaodaima码emory 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 ) {<br /> return strtoupper($matches[0]);<br /> }, 'abdfrew');<br />echo $content;<br />
与之等价的分立代码为
$content = 'abdfrew';<br />preg_match_all('/[a-z]/', $content, $matches );<br />foreach($matches[0] as $v) $r[$v] = strtoupper($v);<br />$content = strtr($content, $r);<br />echo $content;
先分别运行一下,看有无问题