Preg_match apache登录到PHP数组

时间:2016-07-22 16:40:27

标签: php regex apache preg-match

我想在PHP数组中获取我的apache日志以将它们放入数据库中,我尝试了这段代码:

$log = file_get_contents("path/to/my/apache/access_log");
$regex = '/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) "([^"]*)" "([^"]*)"$/';
preg_match($regex, $log, $matches);
print_r($matches);

它没有用,阵列是空的,但我不知道为什么。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我能够将该行与

匹配
/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+)/gm

注意末尾的m(多行)ang g(全局)标志。您可能还需要使用preg_match_all函数,或者更好地一次处理一行文件。