根据Last closing括号分解字符串

时间:2017-07-13 05:07:38

标签: php

我想基于某些条件爆炸一个字符串。

我尝试了很多方法,但无法使其正常工作。

字符串

Thu Jul 13 10:45:37 2017:rlm_perl:PPPoE.Auth.INFO:educationhome,NAS_IP 133.1.92.86,PAY_PLAN 0,CLIENT_MAC 80:2A:A8:6C:F6:14)Thu Jul 13 10:45 :37 2017年:Auth:登录确定:[educationhome](来自客户端mikrotik端口15833926 cli 80:2A:A8:6C:F6:14)Thu Jul 13 10:45:44 2017:Auth:用户无效(帐户已过期[到期2017年7月9日]):[chudamanimrg](来自客户端mikrotik port 16076961 cli 80:2A:A8:0E:37:FE)

预期输出

array(
'Thu Jul 13 10:45:37 2017 : rlm_perl: PPPoE.Auth.INFO: educationhome, NAS_IP 113.1.92.86, PAY_PLAN 0, CLIENT_MAC 80:2A:A8:6C:F6:14',
'Thu Jul 13 10:45:37 2017 : Auth: Login OK: [educationhome] (from client mikrotik port 15833926 cli 80:2A:A8:6C:F6:14)',
'Thu Jul 13 10:45:44 2017 : Auth: Invalid user (Account has expired [Expiration 09 Jul 2017]): [chudamanimrg] (from client mikrotik port 16076961 cli 80:2A:A8:0E:37:FE)'
);

我尝试使用当前日Thr对其进行爆炸,但之后它不适用于旧日志('Wed' Tue' ....)

$day = date("D");
$lines = explode($day,$lines);

我想根据当天的名称

分解字符串$lines
$arr = ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'];

我无法正确地做到这一点。

1 个答案:

答案 0 :(得分:1)

使用preg_splitarray_chunk函数的解决方案:

$lines = 'Thu Jul 13 10:45:37 2017 : rlm_perl: PPPoE.Auth.INFO: educationhome, NAS_IP 103.1.92.86, PAY_PLAN 0, CLIENT_MAC 80:2A:A8:6C:F6:14 Thu Jul 13 10:45:37 2017 : Auth: Login OK: [educationhome] (from client mikrotik port 15833926 cli 80:2A:A8:6C:F6:14) Thu Jul 13 10:45:44 2017 : Auth: Invalid user (Account has expired [Expiration 09 Jul 2017]): [chudamanimrg] (from client mikrotik port 16076961 cli 80:2A:A8:0E:37:FE) ';
$days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];

$result = preg_split('/\b(' . implode('|', $days). ')\b/', $lines, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$result = array_map(function($v){
    return $v[0] . $v[1];
}, array_chunk($result, 2));

print_r($result);

输出:

Array
(
    [0] => Thu Jul 13 10:45:37 2017 : rlm_perl: PPPoE.Auth.INFO: educationhome, NAS_IP 103.1.92.86, PAY_PLAN 0, CLIENT_MAC 80:2A:A8:6C:F6:14 
    [1] => Thu Jul 13 10:45:37 2017 : Auth: Login OK: [educationhome] (from client mikrotik port 15833926 cli 80:2A:A8:6C:F6:14) 
    [2] => Thu Jul 13 10:45:44 2017 : Auth: Invalid user (Account has expired [Expiration 09 Jul 2017]): [chudamanimrg] (from client mikrotik port 16076961 cli 80:2A:A8:0E:37:FE) 
)

Demo link