我需要將數據保存在一個表(用於報告,統計等...),所以用戶可以通過時間搜索,用戶代理等我有一個運行每天都讀腳本Apache日誌,然後將其插入到數據庫中。解析Apache日誌在PHP中使用的preg_match
日誌格式:
10.1.1.150 - - [29/September/2011:14:21:49 -0400] "GET /info/ HTTP/1.1" 200 9955 "http://www.domain.com/download/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
我的正則表達式:
preg_match('/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/',$log, $matches);
現在,當我打印:
print_r($matches);
Array
(
[0] => 10.1.1.150 - - [29/September/2011:14:21:49 -0400] "GET /info/ HTTP/1.1" 200 9955 "http://www.domain.com/download/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
[1] => 10.1.1.150
[2] => -
[3] => -
[4] => 29/September/2011
[5] => 14:21:49
[6] => -0400
[7] => GET
[8] => /info/
[9] => HTTP/1.1
[10] => 200
[11] => 9955
[12] => "http://www.domain.com/download/"
[13] => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
)
我得到:"http://www.domain.com/download/"
和相同的用戶代理。如何在正則表達式中擺脫這些"
?獎金(有沒有什麼快捷方式可以輕鬆插入日期/時間)?
感謝
這是個重複的問題#2221636 –
我已經爲此寫了一個簡單的輔助類。請參閱https://github.com/Spudley/ApacheLogIterator – SDC
@SDC:感謝Simon,該迭代器非常棒! – Pete855217