?
Solved

Extracting data from a line - php

Posted on 2011-02-11
6
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 111

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 111

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 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