Created
April 29, 2023 12:38
-
-
Save ulrischa/43f191ad1388b9bccd0ca3605633b0ad to your computer and use it in GitHub Desktop.
Did you mean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function didYouMean($query, $suggestions) { | |
$querySoundex = soundex($query); | |
$similarSuggestions = []; | |
foreach ($suggestions as $suggestion) { | |
if (soundex($suggestion) == $querySoundex) { | |
$similarSuggestions[] = $suggestion; | |
} | |
} | |
return $similarSuggestions; | |
} | |
$query = "exmple"; | |
$suggestions = ["example", "sample", "exemplary", "exempt"]; | |
$didYouMean = didYouMean($query, $suggestions); | |
if (!empty($didYouMean)) { | |
echo "Did you mean: " . implode(', ', $didYouMean) . "?"; | |
} else { | |
echo "No suggestions found."; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment