function getSpellingSearches($searchTerm) { //First check for things we don't want to load spelling suggestions for if (is_numeric($searchTerm)) { return array(); } if (strpos($searchTerm, '(') !== FALSE || strpos($searchTerm, ')') !== FALSE) { return array(); } if (preg_match('/http:|mailto:|https:/i', $searchTerm)) { return array(); } if (strlen($searchTerm) >= 256) { return array(); } $spellingWord = new SpellingWord(); $words = explode(" ", $searchTerm); $suggestions = array(); foreach ($words as $word) { //First check to see if the word is spelled properly $wordCheck = new SpellingWord(); $wordCheck->word = $word; if (!$wordCheck->find()) { //This word is not spelled properly, get suggestions for how it should be spelled $suggestionsSoFar = $suggestions; $wordSuggestions = $spellingWord->getSpellingSuggestions($word); foreach ($wordSuggestions as $suggestedWord) { $newSearch = str_replace($word, $suggestedWord, $searchTerm); $searchInfo = new SearchStatNew(); $searchInfo->phrase = $newSearch; $numSearches = 0; if ($searchInfo->find(true)) { $numSearches = $searchInfo->numSearches; } $suggestions[str_pad($numSearches, 10, '0', STR_PAD_LEFT) . $newSearch] = array('phrase' => $newSearch, 'numSearches' => $numSearches, 'numResults' => 1); //Also try replacements on any suggestions we have so far foreach ($suggestionsSoFar as $tmpSearch) { $newSearch = str_replace($word, $suggestedWord, $tmpSearch['phrase']); $searchInfo = new SearchStatNew(); $searchInfo->phrase = $newSearch; $numSearches = 0; if ($searchInfo->find(true)) { $numSearches = $searchInfo->numSearches; } $suggestions[str_pad($numSearches, 10, '0', STR_PAD_LEFT) . $newSearch] = array('phrase' => $newSearch, 'numSearches' => $numSearches, 'numResults' => 1); } } } } krsort($suggestions); //Return up to 10 results max if (count($suggestions) > 10) { $suggestions = array_slice($suggestions, 0, 10); } return $suggestions; }
function getSpellingSearches($searchTerm) { $spellingWord = new SpellingWord(); $suggestions = $spellingWord->getSpellingSuggestions($searchTerm); //Return up to 10 results max if (count($suggestions) > 10) { $suggestions = array_slice($suggestions, 0, 10); } return $suggestions; }