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

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的访问日志。我想正确格式化字符串,以便我可以将其显示在 table 中,这样会更容易阅读。

试试:

\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