Link to home
Start Free TrialLog in
Avatar of 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 ? - - [04/Jul/2010:20:14:18 +0000] "GET HTTP/1.1" 200 4184 "" "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


ip =>,
date => 04/Jul/2010:20:14:18
url =>
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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
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


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


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


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.