gech02ab Posted October 3, 2009 Share Posted October 3, 2009 how to displays the words that the user enters into the textarea, along with the number of occurrences of each word. this time, I eat this this 2 occurrence time 1 occurrence I 1 occurrence eat 1 occurrence Quote Link to comment https://forums.phpfreaks.com/topic/176360-how-to-display-the-word-from-stringnon-repeatitive/ Share on other sites More sharing options...
Mchl Posted October 3, 2009 Share Posted October 3, 2009 You could for example explode it and array_count_values it. Quote Link to comment https://forums.phpfreaks.com/topic/176360-how-to-display-the-word-from-stringnon-repeatitive/#findComment-929535 Share on other sites More sharing options...
RussellReal Posted October 3, 2009 Share Posted October 3, 2009 <?php $string = "this time, I eat this"; $string = preg_replace("/[^a-z1-0 ]/i",'',$string); $a = explode(' ',$string); $words = array(); while ($word = each($a)) { if (!array_key_exists($word,$words)) $words[$word] = 1; else $words[$word]++; } foreach ($words as $word => $count) { echo "$word appears $count time".(($count > 1)? 's':'')."\n<br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176360-how-to-display-the-word-from-stringnon-repeatitive/#findComment-929536 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.