Compute the Levenshtein distance between two strings:
<?php echo levenshtein ( " Hello World " , " ello World " ) ; echo " <br> " ; echo levenshtein ( " Hello World " , " ello World " , 10 , 20 , 30 ) ; ?>The levenshtein() function returns the Levenshtein distance between two strings.
Levenshtein distance, also known as edit distance, refers to the minimum number of edit operations required to convert one string into another between two strings. Permitted editing operations include replacing one character with another, inserting a character, and deleting a character.
By default, PHP gives equal weight to every operation (replacement, insertion, and deletion). However, you can define the cost of each operation by setting the optional insert, replace, and delete parameters.
Note: The levenshtein() function is case-insensitive.
Note: The levenshtein() function is faster than the similar_text() function. However, the similar_text() function provides more accurate results with fewer modifications required.
levenshtein( string1,string2,insert,replace,delete )
parameter | describe |
---|---|
string1 | Required. The first string to compare. |
string2 | Required. The second string to compare. |
insert | Optional. The cost of inserting a character. The default is 1. |
replace | Optional. The cost of replacing a character. The default is 1. |
delete | Optional. The cost of deleting a character. The default is 1. |
Return value: | Returns the Levenshtein distance between the two argument strings, or -1 if one of the strings exceeds 255 characters. |
---|---|
PHP version: | 4.0.1+ |