Jump to content

stuck trying to parse txt file


skillednerd

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.