newbtophp Posted January 29, 2010 Share Posted January 29, 2010 Well i was given a task to rearrange/format words too related keywords, a visitor would submit a list of keywords and can also submit keywords to ignore (optionally - in another textarea), then it would sort the keywords by two word phrase; top recurring keywords which would give the output. When i was assigned the task this example was given: INPUT KEYWORDS (Via $_POST ->new line per word): real property Miami Miami real property for sale Miami homes for sale homes in Miami new homes in Miami Miami houses for sale Miami houses houses in Miami real property Los Vegas Los Vegas real property for sale Los Vegas homes for sale homes in Los Vegas new homes in Los Vegas Los Vegas houses for sale Los Vegas houses houses in Los Vegas IGNORED KEYWORDS (Via $_POST ->new line per word): Miami Los Vegas OUTPUT (comma separated && new line per word): real property,real property Miami real property,real property Los Vegas real property,Miami real property for sale real property,Bevery Hills real property for sale homes,homes in Miami homes,homes in Los Vegas homes,new homes in Miami homes,new homes in Los Vegas houses,Miami houses for sale houses,Miami houses houses,Los Vegas houses for sale houses,Los Vegas houses houses,houses in Miami houses,houses in Los Vegas The trouble, is i need your help to think of how I could achieve, this ie. the methodology behind this?. I've already started it but im getting to the point where im lost on how to get the output (like the example above). My code: <?php if(isset($_POST['submit'])){ if(!empty($_POST['keywords'])){ $words = str_replace(array("\r\n", "\r"), "\n", $_POST['keywords']); //words array $wordsArray = explode("\n", $words); $words2 = str_replace(array("\r\n", "\r"), "\n", $_POST['ignorewords']); //ignore words array $wordsArray2 = explode("\n", $words2); //remove found ignore'd words for words array foreach(array_intersect($wordsArray, $wordsArray2) as $match) { foreach(array_keys($wordsArray, $match) as $key) { unset($wordsArray[$key]); } } $comma_separated = implode(",", $wordsArray); $words = array_count_values(str_word_count($comma_separated, 1)); arsort($words); $words = array_slice($words, 0, 2); //this gives the top 2 recurring words. print_r($words); } } ?> All help is appreciated. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/190249-how-would-this-work/ 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.