Monday, April 20, 2009

The levenshtein() function

This function calculates the Levenshtein distance between two strings.

syntax: levenshtein(string1,string2[,insert,replace,delete]);

string1 and string2 are the two input strings.
insert- the cost of inserting a character
replace - the cost of replacing a character
delete - the cost of deleting a character

By default, the cost for each operation is 1. However, we could also mention a cost, in which case, the Levenshtein cost would be calculated accordingly.

Example input:

$str1 = "John";
$str2 = "COrney";
echo levenshtein($str1,$str2);
echo "
";
echo levenshtein($str1,$str2,1,2,3);


Example output:

5
8

What is Levenshtein distance?

The Levenshtein distance between two strings is the minimum number of operations required to convert one string to another.

The operations might include insert, replace or a delete.

No comments: