note 37705 deleted from function.preg-match by tularis

From: Date: Thu, 03 Jul 2008 21:40:26 +0000
Subject: note 37705 deleted from function.preg-match by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141152@lists.php.net to get a copy of this message
Note Submitter: thivierr at telus dot net ---- A web server log record can be parsed as follows: $line_in = '209.6.145.47 - - [22/Nov/2003:19:02:30 -0500] "GET /dir/doc.htm HTTP/1.0" 200 6776 "http://search.yahoo.com/search?p=key+words=UTF-8" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"'; if (preg_match('!^([^ ]+) ([^ ]+) ([^ ]+) \[([^\]]+)\] "([^ ]+) ([^ ]+) ([^/]+)/([^"]+)" ([^ ]+) ([^ ]+) ([^ ]+) (.+)!', $line_in, $elements)) { print_r($elements); } Array ( [0] => 209.6.145.47 - - [22/Nov/2003:19:02:30 -0500] "GET /dir/doc.htm HTTP/1.0" 200 6776 "http://search.yahoo.com/search?p=key+words=UTF-8" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)" [1] => 209.6.145.47 [2] => - [3] => - [4] => 22/Nov/2003:19:02:30 -0500 [5] => GET [6] => /dir/doc.htm [7] => HTTP [8] => 1.0 [9] => 200 [10] => 6776 [11] => "http://search.yahoo.com/search?p=key+words=UTF-8" [12] => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)" ) Notes: 1) For the referer field ($elements[11]), I intentially capture the double quotes (") and don't use them as delimiters, because sometimes double-quotes do appear in a referer URL. Double quotes can appear as %22 or \". Both have to be handled correctly. So, I strip off the double quotes in a second step. 2) The URLs should be further parsed, using parse_url, which is quicker and more reliable then preg_match. 3) I assume the requested protocol (HTTP/1.1) always has a slash character in the middle, which might not always be the case, but I'll take the risk. 4) The agent field ($elments[12]) is the most unstructured field, so I make no assumptions about it's format. If the record is truncated, the agent field will not be delimited properly with a quote at the end. So, both cases must be handled. 5) A hyphen (- or "-") means a field has no value. It is necessary to convert these to appropriate value (such as empty string, null, or 0). 6) Finally, there should be appropriate code to handle malformed web log enteries, which are common, due to junk data. I never assume I've seen all cases.

« previous php.notes (#141152) next »