How to parse Apache logs using a regex in PHP

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

    // # Parses the NCSA Combined Log Format lines:
    $pattern = '/^([^ ]+) ([^ ]+) ([^ ]+) (\[[^\]]+\]) "(.*) (.*) (.*)" ([0-9\-]+) ([0-9\-]+) "(.*)" "(.*)"$/';
    

    Usage:

    if (preg_match($pattern,$yourstuff,$matches)) {
    
        //# puts each part of the match in a named variable
    
        list($whole_match, $remote_host, $logname, $user, $date_time, $method, $request, $protocol, $status, $bytes, $referer, $user_agent) = $matches;
    
    }
    

提交回复
热议问题