How to parse Apache logs using a regex in PHP

后端 未结 4 606
终归单人心
终归单人心 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:29

    You should check out a regular expression tutorial. But here is the answer:

    if (preg_match('/^(\S+) \S+ \S+ \[(.*?)\] "(\S+).*?" \d+ \d+ "(.*?)" "(.*?)"/', $line, $m)) {
      $ip = $m[1];
      $date = $m[2];
      $method = $m[3];
      $referer = $m[4];
      $browser = $m[5];
    }
    

    Take care, it's not the domain name in the log but the HTTP referer.

提交回复
热议问题