This article mainly introduces the php Method for querying strings with the highest similarity. It involves php's skill in operating strings and arrays to implement similarity algorithms. It has some reference value. For more information, see
This article mainly introduces the php Method for querying strings with the highest similarity. It involves php's skill in operating strings and arrays to implement similarity algorithms. It has some reference value. For more information, see
This example describes how to query strings with the highest similarity in php. Share it with you for your reference. The details are as follows:
Returns the string with the highest similarity in the array based on the input string and array.
1. the 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. Sample Code:
// Return the highest similarity state name $ united_state_list = array ('al' => "Alabama ", 'ak' => "Alaska", 'az' => "Arizona", 'ar '=> "Arkansas", 'CA' => "California ", 'co' => "Colorado", 'ct '=> "Connecticut", 'de' => "Delaware", 'dc' => "District Of Columbia ", 'fl '=> "Florida", 'gal' => "Georgia", 'Hi' => "Hawaii", 'id' => "Idaho ", 'Il '=> "Illinois", 'in' => "Indiana", 'ia' => "Iowa", 'K' => "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 Lina", 'nd' => "North Dakota", 'oh' => "Ohio",' OK '=> "Oklahoma ", 'OR' => "Oregon", 'pa' => "Pennsylvania", 'ri' => "Fig Island", 'scs' => "South Carolina Lina ", 'sd' => "South Dakota", 'tn' => "Tennessee", 'tx '=> "Texas", 'ut' => "Utah ", 'vt '=> "Vermont", 'va' => "Virginia", 'wa '=> "Washington", 'wv' => "West Virginia ", 'wy' => "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 php programming.
,