Solved

Extracting data from a line - php

Posted on 2011-02-11
6
238 Views
Last Modified: 2012-05-11
This lines is from a log file. It logs the http requests it gets for every file it has and produce this log file.  Is there a way to break this file to an array, an assoc array ?

68.68.108.4 - - [04/Jul/2010:20:14:18 +0000] "GET http://cd.dweb.com/hand.jpg HTTP/1.1" 200 4184 "http://sdpics.com/" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.453.1 Safari/534.2"

Open in new window


like

ip => 68.68.108.4,
date => 04/Jul/2010:20:14:18
url => http://cd.dweb.com/hand.jpg
browser => Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.453.1 Safari/534.2


Thank you
0
Comment
Question by:sahanz
  • 3
  • 3
6 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34878324

<?php // RAY_temp_sahanz.php
error_reporting(E_ALL);
echo "<pre>";

$txt = <<<ENDTEXT
68.68.108.4 - - [04/Jul/2010:20:14:18 +0000] "GET http://cd.dweb.com/hand.jpg HTTP/1.1" 200 4184 "http://sdpics.com/" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.453.1 Safari/534.2"
ENDTEXT;

// WANTED: AN ASSOCIATIVE ARRAY
/*
ip => 68.68.108.4,
date => 04/Jul/2010:20:14:18
url => http://cd.dweb.com/hand.jpg
browser => Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.453.1 Safari/534.2
*/

$arr = array();

// IP ADDRESS COMES FIRST, ENDS IN BLANK
$x = explode(' ', $txt);
$arr["ip"] = $x[0];

// DATE IS FOUND INSIDE BRACKETS
$x = explode('[', $txt);
$x = explode(']', $x[1]);
$arr["date"] = $x[0];

// URL IS FOUND AFTER "GET AND BOUNDED BY BLANKS
$x = explode("GET", $txt);
$x = trim($x[1]);
$x = explode(' ', $x);
$arr["url"] = $x[0];

// BROWSER IS THE LAST THING, ENCLOSED IN QUOTES
$x = strrev($txt);
$x = trim($x, '"');
$x = explode('"', $x);
$arr["browser"] = strrev($x[0]);

// SHOW WHAT WE GOT
var_dump($txt);
var_dump($arr);

Open in new window

0
 
LVL 1

Author Comment

by:sahanz
ID: 34878340
whoa, i thought it'd be a regex solution
0
 
LVL 1

Author Closing Comment

by:sahanz
ID: 34878351
Working perfect, next time i'll try to do like this.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34878433
I often find that it takes longer to debug REGEX solutions than it does to write simpler code.  Nothing wrong with REGEX, but a language made up of nothing but punctuation is very hard to get right.  Going forward, you might want to package this code into a function.  You feed the function a string and it returns the array.  Then in your mainline code, all you would need to write is a single line, something like:

$arr = parse_my_log_text($txt);

Thanks for the points and best of luck with the project, ~Ray
0
 
LVL 1

Author Comment

by:sahanz
ID: 34878447
Yes, I learn new stuff but wasn't able go near regex even if I tried few times, thanks for the tip.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34878631
Ha!  I know what you mean.  There are entire books devoted to REGEX.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question