将访问日志字符串转换为数组


Convert access log strings to array

聪明的头脑,请帮我拆分这个字符串。我想使用preg_split将其转换为数组,但我无法获得正确的正则表达式。

time:27/Mar/2015:17:56:12 +0900 host:210.210.210.210    user:-  forwardedfor:-  req:-   method:-    uri:-   protocol:-  status:200  size:0  reqsize:0   referer:-ua:-   vhost:www.web.com   reqtime:59.992  cache:- apptime:-   https:  session_id: 

要求:

array(
    'time' => '27/Mar/2015:17:56:12 +0900',
    'host' => '210.210.210.210',
    'user' => '-',
    'forwardedfor' => '-',
    'req' => '-',
    'method' => '-',
    'uri' => '-',
    'protocol' => '-',
    'status' => '200',
    'size' => '0',
    'reqsize' => '0',
    'referer' => '-',
    'ua' => '-',
    'vhost' => 'www.web.com',
    'reqtime' => '59.992',
    'cache' => '-',
    'apptime' => '-',
    'https' => '',
    'session_id' => ''
)

实际上这是来自nginx的访问日志。我想正确地格式化字符串,这样我就可以在表中显示它,这样更容易阅读。

尝试使用:

's*time:(.*?)'s*host:(['d'.]{0,15})'s*user:(.*?)'s*forwardedfor:(.*?)'s*req:(.*?)'s*method:(.*?)'s*uri:(.*?)'s*protocol:(.*?)'s*status:('d*)'s*size:('d*)'s*reqsize:('d*)'s*referer:(.*?)'s*ua:(.*?)'s*vhost:(.*?)'s*reqtime:(['d'.]*)'s*cache:(.*?)'s*apptime:(.*?)'s*https:(.*?)'s*session_id:(.*?)'s*

并相应地提取每组。

Regex101:https://regex101.com/r/jK7rC2/1

$regex = "'s*time:(.*?)'s*host:(['d'.]{0,15})'s*user:(.*?)'s*forwardedfor:(.*?)'s*req:(.*?)'s*method:(.*?)'s*uri:(.*?)'s*protocol:(.*?)'s*status:('d*)'s*size:('d*)'s*reqsize:('d*)'s*referer:(.*?)'s*ua:(.*?)'s*vhost:(.*?)'s*reqtime:(['d'.]*)'s*cache:(.*?)'s*apptime:(.*?)'s*https:(.*?)'s*session_id:(.*?)'s*";
if (preg_match_all($regex, $input_string, $matches_out)) {
   $_time = $matches_out[1];
   $_host = $matches_out[2];
   $_user = $matches_out[3];
   .....
}

有关组的更多信息:http://regexone.com/cheatsheet