Revision: 21789
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 21, 2009 06:53 by mpontus
Initial Code
function parse_access_log_line($format, $line) {
// Get list of consistent patterns
$patterns = array();
while($res = preg_match('/(([^%]*)%[^ ]*([a-z]))([^%]*)/i', $format, $matches)) {
$patterns[$matches[3]] = '('.preg_quote($matches[2]).'(.+?))'.preg_quote($matches[4]);
// Shift format string
$format = str_replace($matches[1], '', $format);
}
// Add beginning of the string to the first pattern and end to the last
$keys = array_keys($patterns);
$patterns[$keys[0]] = '^'.$patterns[$keys[0]];
$patterns[end($keys)] .= '$';
$result = array();
// Consistently apply patterns to the log line shifting it to the right
foreach ($patterns as $node => $pattern) {
preg_match('/'.$pattern.'/', $line, $matches);
$line = str_replace($matches[1], '', $line);
$result[$node] = $matches[2];
}
return $result;
}
Initial URL
Initial Description
I written this code to parse apache log in common log formats. It may have problems with irresponsible formatting like non-concluded into quotes request line. I welcome any modifications to my snippet because it was written if short time and this is my first snippet I publish here. Thanks.
Initial Title
Apache log line parser
Initial Tags
format, apache, log
Initial Language
PHP