Solved

Extracting data from a line - php

Posted on 2011-02-11
6
236 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 look for a specific file type in a local or remote server directory using PHP.

920 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

17 Experts available now in Live!

Get 1:1 Help Now