lopez86100 Posted December 31, 2006 Share Posted December 31, 2006 I've got big text in a string. I don't know how to do that: I want to find 3 words that are most repeated in this string. I'm creating search engine for my website so I need it. Can anyone help me. Maybe there are some ready solutions (someone has created it before me) but I can't find it. please... please.... help me :) :) Link to comment https://forums.phpfreaks.com/topic/32410-searching-for-repeated-words-in-string/ Share on other sites More sharing options...
spelltwister Posted December 31, 2006 Share Posted December 31, 2006 I would explode the sentance by the space, causing each individual word to be placed into an array, then you can filter out words that are smaller than 3 letters or something and then you can sort the array. After the array is sorted, you can easily find which are repeated the most. Link to comment https://forums.phpfreaks.com/topic/32410-searching-for-repeated-words-in-string/#findComment-150529 Share on other sites More sharing options...
kenrbnsn Posted January 1, 2007 Share Posted January 1, 2007 As the above poster suggests, use the [url=http://www.php.net/explode]explode()[/url] function to get all the words in the sentence into an array, then use the function [url=http://www.php.net/array_count]array_count()[/url] to get the frequency of each word in another array. You can then sort that array.Ken Link to comment https://forums.phpfreaks.com/topic/32410-searching-for-repeated-words-in-string/#findComment-150792 Share on other sites More sharing options...
Orio Posted January 1, 2007 Share Posted January 1, 2007 [code]<?php //$str holds the string$words = str_word_count($str, 1);$frq = array_count_values($words);asort($frq);foreach ($frq as $word => $times) echo $word." was found ".$times." times.<br>";?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/32410-searching-for-repeated-words-in-string/#findComment-150796 Share on other sites More sharing options...
lopez86100 Posted January 1, 2007 Author Share Posted January 1, 2007 Thanks , it's so easy - when I'm reading this now. One more time Thanks guys. Link to comment https://forums.phpfreaks.com/topic/32410-searching-for-repeated-words-in-string/#findComment-150981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.