I’m trying to find out the ‘best’ option for using levenshtein() in PHP.
I’m using a PDO statement when a user enters a new customer into the DB.
What I want to do is before the new Customer is added have a query search and see if that Customer is already in the database to avoid a possible duplicate entry. I can’t use MATCH/AGAINST as the Customer may have been entered differently either time.
This will check the First Name against the Database….
$input1 = $_POST['FIRSTNAME'];
$result = array();
$count=1;
$find = $dbh->query('SELECT BillingFirstName FROM Customers');
while ($row = $find->fetch (PDO::FETCH_ASSOC))
{
$fn = $row['BillingFirstName'];
array_push($result, $fn);
}
foreach ($result as $word) {
echo $word.'<br>';
$shortest = -1;
$lev = levenshtein($input1, $word);
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input1\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
How would I add the rest of the details, like Last Name, Address, State, Zip.
Can I even add more or would I need to create the array and loop it with each part of the address?
that would be incredibly slow especially if the db is of any size, what you should use is a levenshtein function in MySQL(not php)
here is a good one: Levenshtein distance