2013-03-14 115 views
3

preg_split('#(?=.)(?<=.)#u','asfaaasfdf');字符串爲固定大小的塊產生:劈裂僅使用正則表達式

Array 
(
    [0] => a 
    [1] => s 
    [2] => f 
    [3] => a 
    [4] => a 
    [5] => a 
    [6] => s 
    [7] => f 
    [8] => d 
    [9] => f 
) 

我怎麼只能改變正則表達式,並得到:

Array 
(
    [0] => as 
    [1] => fa 
    [2] => aa 
    [3] => sf 
    [4] => df 
) 

或:

Array 
(
    [0] => asf 
    [1] => aaa 
    [2] => sfd 
    [3] => f 
) 

回答

2

爲什麼使用拆分?使用比賽:

preg_match_all('/.{1,3}/s', 'asfaaasfdf', $matches); 
print_r($matches[0]); 

輸出:

Array 
(
    [0] => asf 
    [1] => aaa 
    [2] => sfd 
    [3] => f 
)