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]; 

working link


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -