dadamssg Posted August 27, 2009 Share Posted August 27, 2009 I have a a sentence $sentence = "The quick brown fox jumped over the gate"; how would put each of those words as an individual item in an array? so if you print the array it would look like this Array ( [0] => The [1] => quick [2] => brown [3] => fox [4] => jumped [8] => over [9] => the [10]=> gate ) Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/ Share on other sites More sharing options...
Andy-H Posted August 27, 2009 Share Posted August 27, 2009 $sentence = "The quick brown fox jumped over the gate"; $sentence = explode(' ', $sentence); echo '<pre>' . print_r($sentence, true) . '</pre>'; explode print_r Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/#findComment-907596 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 or: print_r(str_word_count("The quick brown fox jumped over the gate", 1)); Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/#findComment-907597 Share on other sites More sharing options...
Andy-H Posted August 27, 2009 Share Posted August 27, 2009 Nice, never knew that function existed. Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/#findComment-907600 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Nice, never knew that function existed. It's name does tend to fool anyone (it even almost fooled me) not reading the manual properly. As it contains a format (hence the 1 in my function call) to set the type of return. Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/#findComment-907618 Share on other sites More sharing options...
Andy-H Posted August 27, 2009 Share Posted August 27, 2009 Yeh I just manual'd it lol The 2nd parameter set as 2 could be useful, not in this case tho. =p Quote Link to comment https://forums.phpfreaks.com/topic/172136-put-words-from-a-string-as-indivual-items-in-array/#findComment-907620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.