How to parse Apache logs using a regex in PHP

后端 未结 4 605
终归单人心
终归单人心 2021-02-09 17:00

I\'m trying to split this string in PHP:

11.11.11.11 - - [25/Jan/2000:14:00:01 +0100] \"GET /1986.js HTTP/1.1\" 200 932 \"http://domain.com/index.html\" \"Mozill         


        
4条回答
  •  鱼传尺愫
    2021-02-09 17:25

    This log format seems to be the Apache’s combined log format. Try this regular expression:

    /^(\S+) \S+ \S+ \[([^\]]+)\] "([A-Z]+)[^"]*" \d+ \d+ "[^"]*" "([^"]*)"$/m
    

    The matching groups are as follows:

    1. remote IP address
    2. request date
    3. request HTTP method
    4. User-Agent value

    But the domain is not listed there. The second quoted string is the Referer value.

提交回复
热议问题