Re: fail on preg_match_all
| From: | Stut | Date: | Thu, 21 Feb 2008 10:52:14 +0000 |
| Subject: | Re: fail on preg_match_all | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-269776@lists.php.net to get a copy of this message | ||
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 }Increasing the memory limit is the worst possible solution to this "problem". It's a giant file, and your regex is basically pulling out each line. For the love of $DEITY learn about fgets and process each line one by one rather than loading in the whole file. It'll be a lot faster and won't suck your memory dry while it runs. -Stut -- http://stut.net/