php - How do you split a string into word pairs? -
i trying split string array of word pairs in php. example if have input string:
"split string word pairs please"
the output array should like
array ( [0] => split [1] => string [2] => string [3] => word [4] => word pairs [5] => pairs please [6] => please )
some failed attempts include:
$array = preg_split('/\w+\s+\w+/', $string);
which gives me empty array, and
preg_match('/\w+\s+\w+/', $string, $array);
which splits string word pairs doesn't repeat word. there easy way this? thanks.
why not use explode ?
$str = "split string word pairs please"; $arr = explode(' ',$str); $result = array(); for($i=0;$i<count($arr)-1;$i++) { $result[] = $arr[$i].' '.$arr[$i+1]; } $result[] = $arr[$i];
Comments
Post a Comment