Php 将访问日志字符串转换为数组
聪明的头脑,请帮我解开这根绳子。我想使用preg_split将其转换为数组,但无法获得正确的正则表达式Php 将访问日志字符串转换为数组,php,regex,nginx,split,explode,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
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:
关于组的更多信息:您能告诉我,在每个名称和值之后,字符串中都会有一个空格吗?@anantkumarsingh我试图执行这个print\r(preg\u split('/{2,}/'),“时间:2015年3月27日:17:56:12+0900主机:210.210.210.210用户:-转发目的:-请求:-方法:-uri:-协议:-状态:200大小:0请求大小:0参考:-ua:-vhost:www.web.com请求时间:59.992缓存:-应用时间:-https:session_id:);在,但结果不正确。explode将是更好的选择
$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];
.....
}