Supposedly, I can use the levenshtein function to test the similarity of words. My word list (garnered from a MYSQL database) is as follows:
Aloha
asd
Broward
Ft Lauderdale
Hollywood
South Florida
Test10
Your Way
my function is:
function closest($seed, $haystack)
{
$shortest = -1;
foreach ($haystack as $word)
{
$lev = levenshtein($seed, $word);
if ($lev == 0)
{
$closest = $word; $shortest = 0; break;
}
if ($lev <= $shortest || $shortest <0)
{
$closest = $word; $shortest = $lev;
}
}
return $closest;
}
I have all the above words in an array called $words. I'm calling the function like this:
$s = closest($m, $words);
where $m is the value of the word to search for. I searched for "apple" and got "ASD"; I searched for "yellow" and got "Test10" ... then searched for "you" and got "ASD." I'm trying to get the nearest match, so I can modify the search parameters, i.e., I type "apple" and get both "aloha" and "asd," as being the one below, one above; search for "yellow" would get "Test10" and "Your Way"; the search for "you" would get "Your Way".
Any ideas?