Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extracting data from a line - php

Posted on 2011-02-11
6
Medium Priority
?
247 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

604 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