2017-07-13 36 views
0

我想根據某些條件來分解字符串。根據最後的右括號分解字符串

我嘗試了很多方法,但無法正常工作。

字符串

週四年07月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:登錄OK:[educationhome](來自客戶端mikrotik端口15833926 cli 80:2A:A8:6C:F6:14)Thu Jul 13 10:45:44 2017:Auth :無效用戶(帳戶已過期[過期2017年7月9日]):[chudamanimrg](從客戶的MikroTik端口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當天爆炸,但隨後它wouldnt的舊日誌('Wed' Tue' ....)

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

我想爆炸根據當天的名稱的字符串$lines工作

$arr = ['Sun','Mon','Tue','Wed','Thr','Fri','Sat']; 

我無法正確執行此操作。

+0

準確顯示所需的輸出 – peterm

+0

@peterm我沒有更新的問題。 – prakashchhetri

+0

使用正則表達式 – AlexM

回答

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

相關問題