Php Method for querying strings with the highest similarity

Source: Internet
Author: User
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.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.