Re: fail on preg_match_all

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hamilton Turner wrote:
Just a follow-up on this, the problem was 'Fatal error: Allowed memory size of 8388608 bytes exhausted'

After some nice help, I found that this is actually a common problem with intense regexes in php. Quick fix is using ini_set() to increase your memory_limit to something massive, like '400M'. This gives your script access to that much memory for its life-time. If you have this problem, then you probably also have to do this

set_time_limit(0);  //remove any max execution time

Hamilton


PS - for anyone confused, here was the script . . . i didnt think it was that confusing, sorry guys!

function parse_access($file_name)
{
   // read file data into variable
   $fh = fopen($file_name, 'r') or die("cant open file for reading");
   $theData = fread($fh, filesize($file_name));
   fclose($fh);

   // perform regex
$regex = '!(\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}) - - \[(\d{2})/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Aug|Oct|Nov|Dec)/(\d{4}):(\d{2}):(\d{2}):(\d{2}) -\d+] {1,4}"GET ([._0-9\-%a-zA-Z/,?=]+) ([.0-9a-zA-Z%/\-,_?]+)" (\d{3}) (\d+) \[(.+?)] \[(.+?)] \[(.+?)] (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)!';
   //echo $regex . '<br><br><hr><br>';
   $num = preg_match_all($regex, $theData, $match, PREG_SET_ORDER);
   //echo "after regex - we are still alive!";

//go on to do some boring stuff, like write this to an array, perform stuff, graph stuff, blah blah
}





Can you also post a few lines from your access log so we've got something to test against.

The regex looks incorrect to me in a few places:
-\d+] {1,4}
for example.

How to debug your script:

make a copy of the log file and trim it down to say 20 lines; run the script on it to verify it's doing what you want it to.

check the size of the "real" log file, then multiply it by 2.5 and see if the total is greater than your php max memory setting. (exp: $theData will hold the full file, matches will also hold another copy of most of the file, then a bit extra for php to use)

Regards

Nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux