PHP nearest string comparison [duplicate]

假如想象 提交于 2019-12-01 01:34:52
hek2mgl

The levenshtein() function will do what you expect. The Levenshtein algorithm calculates the number of insert and replace actions being required to transform some string into another. The result is called an edit distance. The distance can be used to compare strings as you requested.

This example is derived from the documentation of the PHP levenshtein() function.

<?php

$input = 'Director, My Company';

// array of words to check against
$words  = array('Foo bar','Lorem Ispum','Director');

// no shortest distance found, yet
$shortest = -1;

// loop through words to find the closest
foreach ($words as $word) {

    // calculate the distance between the input word,
    // and the current word
    $lev = levenshtein($input, $word);

    // check for an exact match
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $word;
        $shortest = 0;

        // break out of the loop; we've found an exact match
        break;
    }

    // if this distance is less than the next found shortest
    // distance, OR if a next shortest word has not yet been found
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}

echo "Input word: $input\n";
if ($shortest == 0) {
    echo "Exact match found: $closest\n";
} else {
    echo "Did you mean: $closest?\n";
}

Scripts output is

Input word: Director, My Company
Did you mean: Director?

Good Luck!

You can use http://php.net/manual/en/function.levenshtein.php to determine the distance between two strings.

$subj = "Director, My Company";
$str = array();
$str[] = "Foo bar";
$str[] = "Lorem Ipsum";
$str[] = "Director";

$minStr = "";
$minDis = PHP_INT_MAX;
for ($str as $curStr) {
  $dis = levenshtein($subj, $curStr);
  if ($dis < $minDis) {
    $minDis = $dis;
    $minStr = $curStr;
  }
}
echo($minStr);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!