Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* string find_best_match (string $word, array $db, float &$best_match_score);
* Compares $word with each ocurrence in $db, then results the closest matching string.
* Also returns how much both words matched in %.
*/
function find_best_match ($word, $db, &$best_match_score) {
// Initialize our comparison variables
$best_match = $word;
$best_match_score = 0;
$score = 0;
// Main loop
foreach ($db as $possible_match) {
// For now, let's use PHP's built-in similar_text function for testing
$matches = similar_text(strtolower($word), strtolower($possible_match), $score);
// Here we compare our current best match with our current match
if ($score > $best_match_score) {
// If we found a new best match, let's replace it and update our best match score
$best_match = $possible_match;
$best_match_score = $score;
}
}
// Finally, return our results.
return $best_match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.