String Functions
Advertisements

PHP levenshtein() Function

Topic: PHP String ReferencePrev|Next

Description

The levenshtein() function calculates Levenshtein distance between two strings.

The following table summarizes the technical details of this function.

Return Value: 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.
Version: PHP 4.0.1+

Syntax

The basic syntax of the levenshtein() function is given with:

levenshtein(string1, string2, cost_insert, cost_replacement, cost_delete);

In its simplest form the function will take only the two strings as parameter and will calculate just the number of insert, replace and delete operations needed to transform string1 into string2.

The following example shows the levenshtein() function in action.

<?php
// Calculating the distance
echo levenshtein("flour", "flower")."<br>";
echo levenshtein("weight", "wait")."<br>";
echo levenshtein("hour", "our");
?>

Tip: The levenshtein() function is faster than the similar_text() function. However, similar_text() provides better results with less modifications.

Note: The Levenshtein distance is defined as the minimum number of characters you have to replace, insert or delete to transform string1 into string2. The complexity of the algorithm is O(m*n), where n and m are the length of string1 and string2.


Parameters

The levenshtein() function accepts the following parameters.

Parameter Description
string1 Required. Specifies the first string being evaluated for Levenshtein distance.
string2 Required. Specifies the second string being evaluated for Levenshtein distance.
cost_insert Optional. Defines the cost of insertion.
cost_replacement Optional. Defines the cost of replacement.
cost_delete Optional. Defines the cost of deletion.

More Examples

Here're some more examples showing how levenshtein() function actually works:

The following example demonstrates how to search for the closest match of a misspelled word inside an array of words. You can adjust the sensitivity of the result according to your need.

<?php
// Misspelled word
$input = "calender";

// Array of words to check against
$words  = array("seize", "calendar", "foreign", "category", "quiet");

// No shortest distance found yet
$shortest = -1;

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

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

    // Check for an exact match
    if($lev == 0){
        // Current word is closest and exact match
        $closest = $word;
        $shortest = 0;

        // Break out of the loop, since exact match found
        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" . "<br>";

// Specify the sensitivity of the result
if($shortest > 3){
    echo "No appropriate match found." . "<br>";
} elseif($shortest == 0){
    echo "Exact match found: $closest" . "<br>";
} else{
    echo "Did you mean: $closest?";
}
?>
Advertisements
Bootstrap UI Design Templates