I have a string comparison function based on levenshtein but it don't work properly.
function levenshteinTest($input, $array)
{
$shortest = -1;
foreach ($array as $word)
{
$lev = levenshtein($input, $word);
if ($lev == 0)
{
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0)
{
$closest = $word;
$shortest = $lev;
}
}
return $closest;
}
$test=array(
"Richard Bürstmayr",
"Sandra Ebner"
);
var_dump(levenshteinTest("brstmyr", $test); //Sandra Ebner
var_dump(levenshteinTest("rd brstmyr", $test); //Richard Bürstmayr
As you see I get a bad result at the first dump but a good one at the second one. I think the problem has something to do with word length but I can't really figure out how I could fix that. My array values contain all at least two words.