PHP operation string and array implementation similarity algorithm

Source: Internet
Author: User
This article mainly introduces PHP query the most similarity of the string method, involving PHP operation string and array to achieve the similarity algorithm skills, with a certain reference value, the need for friends can refer to the next

This example describes the method of PHP query for the most similar string. Specific as follows:

Returns the most similar string in the array, based on the strings and arrays passed in

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. The code example is as follows:

Returns the state name with the highest similarity, based on the incoming state name (which may have been lost by the customer) $united_state_list = Array (' AL ' = ' Alabama ', ' AK ' = ' Alaska ', ' AZ ' = ' Arizona ' , ' AR ' = ' Arkansas ', ' CA ' = ' California ', ' CO ' = ' Colorado ', ' CT ' = ' Connecticut ', ' DE ' and ' Delaware ', ' DC ' = "District of Columbia", ' FL ' = ' Florida ', ' GA ' = ' Georgia ', ' HI ' and ' Hawaii ', ' ID ' and ' Idaho ', ' IL ' and ' = ' Illinois ", ' in ' + ' Indiana", ' IA ' = ' Iowa ', ' KS ' = ' Kansas ', ' KY ' = ' Kentucky ', ' LA ' and ' Louisiana ', ' ME ' = > "Maine", ' MD ' = ' Maryland ', ' MA ' = ' Massachusetts ', ' MI ' = ' Michigan ', ' MN ' = ' Minnesota ', ' MS ' and ' = ' Mississippi ", ' MO ' =" Missouri ", ' MT ' = ' Montana ', ' NE ' = ' Nebraska ', ' NV ' and ' Nevada ', ' NH ' = ' New ' Hampshire ", ' NJ ' =" New Jersey ", ' NM ' = ' new Mexico ', ' NY ' = ' new York ', ' NC ' = ' North Carolina ', ' ND ' = ' North Dakota ", ' OH ' and" Ohio ", ' OK ' and ' Oklahoma ', ' OR ' = ' Oregon ', ' PA ' and ' 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 ' and ' Wyoming ' ); $input _state = ' Wiscsin '; $state = Closest_word ($input _state, array_values ($united _state_list)); Echo $state;

Summary : The above is the entire content of this article, I hope to be able to help you learn.

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.