0

我正在处理旧网站并更新已弃用的 php 函数。我有以下代码,当我更改eregpreg.

private function stripScriptTags($string) {
    $pattern = array("'\/\*.*\*\/'si", "'<\?.*?\?>'si", "'<%.*?%>'si", "'<script[^>]*?>.*?</script>'si");
    $replace = array("", "", "", "");
    return ereg_replace($pattern, $replace, $string);
}

这是我得到的错误:

Fatal error: Allowed memory size of 10000000 bytes exhausted (tried to allocate 6249373 bytes) in C:\Inetpub\qcppos.com\library\vSearch.php on line 403

那行代码中是否还有其他内容需要与ereg_replace?

4

3 回答 3

1

So your regexes are as follows:

"'\/\*.*\*\/'si"
"'<\?.*?\?>'si"
"'<%.*?%>'si"
"'<script[^>]*?>.*?</script>'si"

Taking those one at a time, you are first greedily stripping out multiline comments. This is almost certainly where your memory problem is coming from, you should ungreedify that quantifier.

Next up, you are stipping out anything that looks like a PHP tag. This is done with a lazy quantifier, so I don't see any issue with it. Same goes for the ASP tag, and finally the script tag.

Leaving aside potental XSS threats left out by your regex, the main issue seems to be coming from your first regex. Try "'\/\*.*?\*\/'si" instead.

于 2012-06-22T14:12:05.853 回答
0

获取您的内存限制值

 ini_get('memory_limit');

并使用memory_get_usage()memory_get_peak_usage()检查您的脚本 (这个需要 php 5.2 或更高版本),如果结果太低,那么您可以将其设置得更高:

 ini_set("memory_limit","128M"); // "8M" before PHP 5.2.0, "16M" in PHP 5.2.0, > "128M"

只需放入您拥有并适用于您的脚本的任何内存即可。请记住,这限制了单个 php 进程;要全局设置它,您需要调整您的 php.ini 文件。显然,如果它需要大量运行,那么将其视为一个猴子补丁并开始重写它以获得更好的内存占用。

有关更多信息,请查看核心 php.ini指令,搜索资源限制

于 2012-06-19T21:56:20.910 回答
0

由于允许更高的内存分配不起作用,以下函数更新如下(因为它们实际上并没有做任何事情但会导致问题):

 private function stripScriptTags($string) 
{
/* $pattern = array("'\/\*.*\*\/'si", "'<\?.*?\?>'si", "'<%.*?%>'si",
   "'<script[^>]*?>.*?</script>'si");
   $replace = array("", "", "", "");
   return ereg_replace($pattern, $replace, $string);
*/
   return $string;
}

private function clearSpaces($string, $clear_enters = true)
{
/*$pattern = ($clear_enters == true) ? ("/\s+/") : ("/[ \t]+/");
  return preg_replace($pattern, " ", trim($string));
*/
  return $string;
}
于 2012-06-22T13:58:46.147 回答