Extracting data from a txt file in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rikkidas
Forum Newbie
Posts: 1
Joined: Thu May 09, 2013 3:19 am

Extracting data from a txt file in php

Post by rikkidas »

I have a log file which is a .txt file which displays
IP ADDRESS, TIME STAMP,FILENAME,HTTP STATUS CODE,BANDWIDTH,USER AGENT

1.)103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
2.)207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"

I need to findout
1. The total number of file requests in the month.
2. The number of file requests from the articles directory.
3. The TOTAL bandwidth consumed by the file requests over the month.
4. The number of requests that resulted in 404 status errors. Display a list of the filenames that produced these 404 errors (try not to repeat filenames if the same wrong filename was requested more than once

i've managed to get the total number of files

<?php
$file="april.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}

fclose($handle);

echo $linecount;

?>
to get the data i know i need to explode the strings into an array and loop through the array to count the information needed but i'm struggling with exploding it properly so it breaks up the array properly
any ideas
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Extracting data from a txt file in php

Post by requinix »

Even with something as simple as explode()ing on spaces you can still get the information you need: every line has the same number of spaces in the same places. Well, except the user agent, but you don't need that so it's not a problem.

Code: Select all

array(
	"103.239.234.105", // ip address
        "--", // something
        "[2007-04-01", // date, remove the leading [
	"00:42:21]", // time, remove the trailing ]
	'"GET', // method, remove the leading "
	"articles/learn_PHP_basics", // path
	'HTTP/1.0"', // protocol, remove the trailing "
	"200", // response code
	"12729", // response length
	"..." // the rest is the user agent
)
Post Reply