【发布时间】:2011-04-22 13:17:04
【问题描述】:
我正在尝试将字符串拆分为 PHP 中的单词对数组。例如,如果您有输入字符串:
"split this string into word pairs please"
输出数组应该是这样的
Array (
[0] => split this
[1] => this string
[2] => string into
[3] => into word
[4] => word pairs
[5] => pairs please
[6] => please
)
一些失败的尝试包括:
$array = preg_split('/\w+\s+\w+/', $string);
这给了我一个空数组,并且
preg_match('/\w+\s+\w+/', $string, $array);
将字符串拆分成单词对但不重复单词。是否有捷径可寻?谢谢。
【问题讨论】:
-
正则表达式并不总是“字符串”的答案