Link to home
Start Free TrialLog in
Avatar of sahanz
sahanz

asked on

Extracting data from a line - php

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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sahanz
sahanz

ASKER

whoa, i thought it'd be a regex solution
Avatar of sahanz

ASKER

Working perfect, next time i'll try to do like this.
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
Avatar of sahanz

ASKER

Yes, I learn new stuff but wasn't able go near regex even if I tried few times, thanks for the tip.
Ha!  I know what you mean.  There are entire books devoted to REGEX.