dreamwest Posted February 14, 2009 Share Posted February 14, 2009 How can i split a string into individual words: Original string: she walks down the street Split string: she<br> walks<br> down<br> the<br> street<br> Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/ Share on other sites More sharing options...
corbin Posted February 14, 2009 Share Posted February 14, 2009 http://php.net/explode Or, if you want to replace \n (or \r\n) with <br>, you could just use http://php.net/str_replace. Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761884 Share on other sites More sharing options...
drisate Posted February 14, 2009 Share Posted February 14, 2009 $words = explode (' ', $string); foreach ($words as $loop){ echo $loop."<br>"; } or echo str_replace(' ', '<br>', $string); cheers Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761885 Share on other sites More sharing options...
Zane Posted February 14, 2009 Share Posted February 14, 2009 There is also str_word_count....which I just found out about. Which only really saves you the trouble of exploding by space....kinda redundant. foreach(str_word_count($words) as $word) echo $word . " \n"; Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761902 Share on other sites More sharing options...
dreamwest Posted February 14, 2009 Author Share Posted February 14, 2009 Im having no luck with this: $search= "smarter than yogi"; foreach(str_word_count($search) as $word) $display= $word; echo $display; I get this error: Warning: Invalid argument supplied for foreach() in /mounted-storage/home/sub/sc-TQLI/XXXXXXXX.com/1.php on line 17 Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761949 Share on other sites More sharing options...
Zane Posted February 14, 2009 Share Posted February 14, 2009 my badd should be foreach(str_word_count($search, 1) as $word) Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761954 Share on other sites More sharing options...
dreamwest Posted February 14, 2009 Author Share Posted February 14, 2009 my badd should be foreach(str_word_count($search, 1) as $word) Tried it but only displays the last word in the string Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761961 Share on other sites More sharing options...
Zane Posted February 14, 2009 Share Posted February 14, 2009 really drisate's approach is a better method...and the most common...I was just pointing out another function. for gits and shiggles. but the reason it's only showing the last one is because you aren't concatenating this $display= $word; needs to be $display .= $word; note the period in front of the equals sign......that's a concat operator Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761968 Share on other sites More sharing options...
Daniel0 Posted February 14, 2009 Share Posted February 14, 2009 $words = explode (' ', $string); foreach ($words as $loop){ echo $loop."<br>"; } FYI, there is a function called implode/join, which does the reverse of explode, so echo implode('<br>', explode(' ', $words)).'<br>'; would do what you are doing. Replacing spaces using str_replace is better in this case though. Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761970 Share on other sites More sharing options...
dreamwest Posted February 14, 2009 Author Share Posted February 14, 2009 Works! Thanks Ive seen the concat operator around but assumed it was a joiner not a loop joiner (similar to while loop) Link to comment https://forums.phpfreaks.com/topic/145158-split-a-string/#findComment-761972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.