Instance
Calculates the Levenshtein distance between two strings:
<?phpecho Levenshtein ("Hello World", "Ello World"), echo "<br>", Echo Levenshtein ("Hello World", "Ello World", 10,20,30);? >
Definition and usage
The Levenshtein () function returns the Levenshtein distance between two strings.
Levenshtein distance, also known as edit distance, refers to the minimum number of editing operations required to convert a string into another string between two strings. Permission edits include replacing one character with another character, inserting a character, and deleting a character.
By default, PHP has the same weights for each operation (replace, insert, and delete). 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 precise results with fewer required modifications.
Grammar
Levenshtein (String1,string2,insert,replace,delete)
Parameter description
String1 required. The first string to compare.
string2 required. The second string to compare.
Insert is optional. The cost of inserting a character. The default is 1.
Replace is optional. The cost of replacing a character. The default is 1.
Delete is optional. The cost of deleting a character. The default is 1.
Technical details
Return value: Returns the Levenshtein distance between two parameter strings and returns 1 if one of the strings exceeds 255 characters.
PHP version: 4.0.1+
If one of the strings exceeds 255 characters, the Levenshtein () function returns-1.
The Levenshtein () function is not case sensitive.
The Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results that require less modification.
Example
<?php Echo Levenshtein ("Hello World", "Ello World"); echo "<br/>"; Echo Levenshtein ("Hello World", "Ello World", 10,20,30); ? >
Output:
1 30