Skip to content

Instantly share code, notes, and snippets.

@ulrischa
Created April 29, 2023 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulrischa/43f191ad1388b9bccd0ca3605633b0ad to your computer and use it in GitHub Desktop.
Save ulrischa/43f191ad1388b9bccd0ca3605633b0ad to your computer and use it in GitHub Desktop.
Did you mean
<?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