hno Posted July 16, 2009 Share Posted July 16, 2009 HI How can split a string into words and store it in a array.For example in the following string ,how can separate words and store them in a array?"aaa bbb ccc" thanks Link to comment https://forums.phpfreaks.com/topic/166158-how-to-split-a-string-into-words/ Share on other sites More sharing options...
RussellReal Posted July 16, 2009 Share Posted July 16, 2009 explode(' ',$string); Link to comment https://forums.phpfreaks.com/topic/166158-how-to-split-a-string-into-words/#findComment-876242 Share on other sites More sharing options...
Amtran Posted July 16, 2009 Share Posted July 16, 2009 explode(' ',$string); +1 Gotta love explode! Link to comment https://forums.phpfreaks.com/topic/166158-how-to-split-a-string-into-words/#findComment-876245 Share on other sites More sharing options...
Daniel0 Posted July 16, 2009 Share Posted July 16, 2009 Or if you're a kind of DIY person: $string = 'aaa bbb ccc'; $words = array(); for ($i = 0, $j = 0, $length = strlen($string); $i < $length; ++$i) { if ($string[$i] == ' ') { $j++; continue; } if (!isset($words[$j])) { $words[$j] = $string[$i]; } else { $words[$j] .= $string[$i]; } } var_dump($words); Just for fun and because I had a little extra time this morning - use explode() instead Link to comment https://forums.phpfreaks.com/topic/166158-how-to-split-a-string-into-words/#findComment-876247 Share on other sites More sharing options...
Ken2k7 Posted July 16, 2009 Share Posted July 16, 2009 strtok() and split() are alternate ways of doing this same thing. But yes, use explode(). =D Link to comment https://forums.phpfreaks.com/topic/166158-how-to-split-a-string-into-words/#findComment-876273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.