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

时间:2015-03-27 10:33:07

标签: php regex nginx split explode

聪明的头脑,请帮我拆分这个字符串。我想使用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的访问日志。我想正确格式化字符串,以便我可以在表格中显示它,因此更容易阅读。

1 个答案:

答案 0 :(得分:0)

尝试:

\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