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
)

我怎样才能只更改 RegEx 并获得:

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

或者:

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

1 回答 1

2

为什么要使用拆分?使用匹配:

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

输出:

Array
(
    [0] => asf
    [1] => aaa
    [2] => sfd
    [3] => f
)
于 2013-03-14T07:28:25.940 回答