让我们说我有一个字符串:
$string = "This is my test case for an example."
Run Code Online (Sandbox Code Playgroud)
如果我根据''得到一个爆炸而爆炸
Array('This','is','my','test','case','for','an','example.');
Run Code Online (Sandbox Code Playgroud)
我想要的是每隔一个空间爆炸:
Array('This is','my test','case for','an example.').
Run Code Online (Sandbox Code Playgroud)
字符串可能有一些奇数字,因此数组中的最后一项可能不包含两个单词.
有人知道怎么做吗?
$matches = array();
preg_match_all('/([A-Za-z0-9\.]+(?: [A-Za-z0-9\.]+)?)/',
'This is my test case for an example.',$matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
收益率:
Array
(
[0] => Array
(
[0] => This is
[1] => my test
[2] => case for
[3] => an example.
)
[1] => Array
(
[0] => This is
[1] => my test
[2] => case for
[3] => an example.
)
)
Run Code Online (Sandbox Code Playgroud)
更新修复它以匹配句子末尾的单个单词