PHP query the highest similarity of the string method _php tips

Source: Internet
Author: User
Tags php programming

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.

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.