skillednerd Posted April 11, 2011 Share Posted April 11, 2011 I have stats log file I want to parse lines from. The lines I want to get are: GET /xxxx/xxxx/xxxx/xxxx/xxx HTTP I am trying to get those lines and the regex works to get them but I cant figure out how to process the results I want to print out each match to the browser. Here is the php file I am using and the output it outputs is: Array ( ) <?php $url = "filex"; $filepointer = fopen($url,"r"); if($filepointer){ while(!feof($filepointer)){ $buffer = fgets($filepointer,4096); $file .= $buffer; } fclose($filepointer); } else { die("Could not open file"); } $regexname = "/^GET(.*)HTTP/i"; preg_match_all('/^GET(.*)HTTP/i',$file,$match); $result = $match[1]; print_r($result) ; ?> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/233399-stuck-trying-to-parse-txt-file/ Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 By default the ^ character matches the start of the string, not the start of a line. So it looks like the first line of your file doesn't match and preg_match_all doesn't match anymore since it's never the start of the string again. What you want to do is set the m modifier which lets ^ match the start of each line. I also added \s+ just to make sure it has at least some whitespace in between GET, the url, and HTTP (and not include them in the subpattern). preg_match_all('/^GET\s+(.*?)\s+HTTP/im',$file,$match); $results = $match[1]; This should really be in the regex forum by the way. Quote Link to comment https://forums.phpfreaks.com/topic/233399-stuck-trying-to-parse-txt-file/#findComment-1200345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.