【问题标题】:Convert access log strings to array将访问日志字符串转换为数组
【发布时间】:2015-03-27 10:33:06
【问题描述】:

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

【问题讨论】:

  • 你能说每个名字和值后面都会有一个空格吗?
  • @anantkumarsingh 我试图执行这个 print_r(preg_split('/ {2,}/', "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: "));在writecodeonline.com/php,但是结果不对。
  • explode 会是更好的选择

标签: php regex nginx split explode


【解决方案1】:

尝试:

\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*

并相应地提取每个组。

正则表达式101: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

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多