Home > Backend Development > PHP Tutorial > preg_replace_callback出现内存泄露。有人解决了么?该怎么处理

preg_replace_callback出现内存泄露。有人解决了么?该怎么处理

WBOY
Release: 2016-06-13 12:07:38
Original
1043 people have browsed it

preg_replace_callback出现内存泄露。有人解决了么?
环境:php5.4.22 +centos 5.4+nginx 
--------------------------------------------------------------------------------------------------------------
下面这个问题发现的:http://bbs.csdn.net/topics/390784375
----------------------------------
同样问题:
http://bbs.csdn.net/topics/390693060
----------------------------------------------------------------------
测试代码:

<br /><?php<br />$content='<php>asdfsadf</php><php>asdfsadf</php><php>asdfsadf</php><php>asdfsadf</php><php>asdfsadf</php><php>asdfsadf</php> testtest';<br />$a=123;<br />$content= preg_replace_callback('/<php(\s*?)>(.*?)<\/php(\s*?)>/is', function($match) use($a){return 123;}, $content);<br />echo $content;<br />
Copy after login

----------------------------------------------------运行结果-----------------------
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 )  {<br />                        return strtoupper($matches[0]);<br />                    }, 'abdfrew');<br />echo $content;<br />
Copy after login

与之等价的分立代码为
$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;
Copy after login

先分别运行一下,看有无问题

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template