PHP nearest string comparison [duplicate]

可紊 提交于 2020-01-11 04:12:04

问题


Possible Duplicate:
String similarity in PHP: levenshtein like function for long strings

I have my subject string

$subj = "Director, My Company";

and a list of multiple strings to be compared:

$str1 = "Foo bar";
$str2 = "Lorem Ipsum";
$str3 = "Director";

What I want to achieve here is to find the nearest string related to $subj. Is it possible to do it?


回答1:


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!




回答2:


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);


来源:https://stackoverflow.com/questions/14421303/php-nearest-string-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!