Solved

Extracting data from a line - php

Posted on 2011-02-11
6
234 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 108

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

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 108

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now