This article illustrates the method of the most similar string in PHP query. Share to everyone for your reference. Specifically as follows:
Returns the most similar string in an array, based on the passed-in string and arrays
1. PHP code is as follows:
function Closest_word ($input, $words) {
$shortest =-1;
foreach ($words 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;
}
2. The code example is as follows:
Returns the name of the state with the highest similarity $united _state_list = Array (' AL ' => "Alabama", ' AK ' => "Alaska", ' AZ ' => "Arizona", according to the incoming state name (possible customer error) , ' AR ' => ' Arkansas ', ' CA ' => ' California ', ' CO ' => ' Colorado ', ' CT ' => ' Connecticut ', ' DE ' => ' Delaware ', ' DC ' => "District of Columbia", ' FL ' => ' Florida ', ' GA ' => ' Georgia ', ' HI ' => ' Hawaii ', ' ID ' => ' Idaho ', ' IL ' => ' Illinois ", ' in ' =>" Indiana ", ' IA ' => ' Iowa ', ' KS ' => ' Kansas ', ' KY ' => ' Kentucky ', ' LA ' => ' Louisiana ', ' ME ' = > "Maine", ' MD ' => ' Maryland ', ' MA ' => ' Massachusetts ', ' MI ' => ' Michigan ', ' MN ' => ' Minnesota ', ' MS ' => ' Mississippi ", ' MO ' =>" Missouri ", ' MT ' => ' Montana ', ' NE ' => ' Nebraska ', ' NV ' => ' Nevada ', ' NH ' => ' New Hampshire ", ' NJ ' =>" New Jersey ", ' NM ' => ' new Mexico ', ' NY ' => ' new York ', ' NC ' => ' North Carolina ', ' ND ' => ' North Dakota ", ' OH ' =>" Ohio ", ' OK ' =>" Oklahoma ", ' OR ' => ' Oregon ', ' PA ' => ' Pennsylvania ', ' RI ' => ' Rhode Island ", ' SC ' =>" South Carolina ", ' SD ' => ' South Dakota ', ' TN ' => ' Tennessee ', 'TX ' => ' Texas, ' UT ' => ' Utah ', ' VT ' => ' Vermont ', ' VA ' => ' Virginia ', ' WA ' => ' Washington ', ' WV ' => ' West
Virginia ", ' WI ' =>" Wisconsin ", ' WY ' =>" Wyoming ");
$input _state = ' Wiscsin ';
$state = Closest_word ($input _state, array_values ($united _state_list)); Echo $state;
I hope this article will help you with your PHP programming.