I have 5000, sometimes more, street address strings in an array. I\'d like to compare them all with levenshtein to find similar matches. How can I do this without looping throug
I think you cannot avoid looping through the array as the levenstein() function takes only strings and not an array as input.
You can do something like:
for($i=0;$i<count($array)-1;$i++)
{
for($j=$i+1;$j<count($array);$j++)
{
$lev = levenshtein($array[$i],$array[$j]);
if($lev == 0)
{
// exact match
}
else if($lev <= THRESHOLD)
{
// similar
}
}
}
$stringA = "this is php programming language";
$stringB = "this is complete programming script in which java php and all other minor languages include";
echo "string 1---->".$stringA."<br />";
echo "string 2---->".$stringB."<br />";
// changing string to arrays
$array1 = explode(' ', $stringA);
$array2 = explode(' ', $stringB);
// getting same element from two strings
$c = array_intersect($array1, $array2);
// changing array to the string
$d=implode(' ',$c);
echo "string same elements---> ".$d."<br />";
// getting difrent element from two arrays
$result = array_diff($array2, $array1);
// changing array to the string
$zem = implode(' ',$result);
if (!empty($zem)) {
echo "string diffrence---> ".$zem."<br />";
} else {
echo "string diffrence--->both strings are same <br />";
}
similar_text($stringA, $d, $p);
echo " similarity between the string is ".$p."% <br />";