Home Menu

levenshtein

Calculate Levenshtein distance between two strings

The Levenshtein distance is defined as the minimal number of characters you have to replace, insert, or delete to transform str1 into str2.

Syntax

levenshtein ( string $str1 , string $str2 ) : int
levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del ) : int

Parameters

str1

One of the strings being evaluated for Levenshtein distance.

str2

One of the strings being evaluated for Levenshtein distance.

cost_ins

Defines the cost of insertion.

cost_rep

Defines the cost of replacement.

cost_del

Defines the cost of deletion.

Return

This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters.

Examples

1

<?

$str1 = 'same';
$str2 = 'same';

$return = levenshtein($str1, $str2);
echo $return;

?>
0

2

<?

$str1 = 'same';
$str2 = 'different';

$return = levenshtein($str1, $str2);
echo $return;

?>
8

3

<?

$str1 = 'same';
$str2 = 'different';
$cost_ins = 1;
$cost_rep = 1;
$cost_del = 1;

$return = levenshtein($str1, $str2, $cost_ins, $cost_rep, $cost_del);
echo $return;

?>
8

4

<?

$str1 = 'same';
$str2 = 'different';
$cost_ins = 1;
$cost_rep = 10;
$cost_del = 100;

$return = levenshtein($str1, $str2, $cost_ins, $cost_rep, $cost_del);
echo $return;

?>
35

5

<?

// input misspelled word
$input = 'carrrot';

// words to check
$words  = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato');

// shortest distance not found
$shortest = -1;

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

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

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

        // exact match found
        // set closest match and shortest distance
        $closest = $word;
        $shortest = $lev;

        // break out of loop
        break;
    }

    // check if current distance is less than shortest distance or if shortest distance not found
    if (($lev <= $shortest) || ($shortest == -1)) {

        // closest match found
        // set closest match and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}

echo "input: $input\n";

if ($shortest == 0) {
    echo "exact match: $closest";
} else {
    echo "closest match: $closest";
}

?>
input: carrrot
closest match: carrot

PHP

JesusBible
HTMLCSSJSPHPSVGMore
AboutTerms