I have a mySQL table where people add their names and their interests. I want to use some kind of phrase that passes and finds either a 100% match or a close match. Ive heard of levenshtein distance, but have no idea how to do this loop through my desk.
$input = $_POST["interest"]; $result = mysql_query("SELECT interest_desc FROM interests");
Did some search engine and got to this point
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; } $array = mysql_fetch_row($result); $closestmatch = closest($input,$array); echo $closetmatch;
source share