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
https://forums.phpfreaks.com/topic/233399-stuck-trying-to-parse-txt-file/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.