PHP levenshtein Function

What is levenshtein Function?

Explanation

In PHP, this function is used to calculate levenshtein distance between two strings.

Syntax:


levenshtein(string1,string2,insert,replace,delete)

In the above syntax "string1" specifies the first string to compare,"string2" specifies the second one to compare, "insert", "replace", "delete" are optional values which refers the cost for insertion, replacement, deletion.

Example :


<?php
$a = "first";
$b = "flirt";
echo levenshtein($a, $b); // 2 (insert "l", delete "s")
echo levenshtein($b, $a); // 2 (delete "l", insert "s")
?>
Result :

22

In the above example when the first statement is executed, the character "l" is inserted, "s" has to be deleted, to tranform the string "flirt" to "first" and it is vice versa in the second statement.The code returns 22 as the result, since two letters are changed in each function.

PHP Topics


Ask Questions

Ask Question