I am trying to split a string into an array of word pairs in PHP. For example, if you have an input string:
"split this string into word pairs please"
output array should look like
Array (
[0] => split this
[1] => this string
[2] => string into
[3] => into word
[4] => word pairs
[5] => pairs please
[6] => please
)
Some unsuccessful attempts include:
$array = preg_split('/\w+\s+\w+/', $string);
which gives me an empty array, and
preg_match('/\w+\s+\w+/', $string, $array);
which breaks the string into pairs of words but does not repeat the word. Is there an easy way to do this? Thank.
source
share