PHP levenshtein () function
Examples
Calculate Levenshtein distance between two strings:
echo levenshtein("Hello World","ello World");
echo "<br>";
echo levenshtein("Hello World","ello World",10,20,30);
?>
Running instance »
Definition and Usage
levenshtein () function returns the Levenshtein distance between two strings.
Levenshtein distance, also known as the edit distance between two strings means that by editing a string into a minimum number of operations needed another string. License editing operation comprises replacing one character to another character, insert a character, delete a character.
By default, PHP for each operation (substitutions, insertions and deletions) the same weight. However, you can set the optional insert, replace, delete parameters to define the cost of each operation.
Note: levenshtein () function is not case-sensitive.
Note: levenshtein () function than similar_text () function faster.However, similar_text () function provides more accurate results with less number of changes required.
grammar
参数 | 描述 |
---|---|
string1 | 必需。要比较的第一个字符串。 |
string2 | 必需。要比较的第二个字符串。 |
insert | 可选。插入一个字符的代价。默认是 1。 |
replace | 可选。替换一个字符的代价。默认是 1。 |
delete | 可选。删除一个字符的代价。默认是 1。 |
technical details
return value: | Back Levenshtein distance between two parameter string, if one of the strings more than 255 characters, -1 is returned. |
---|---|
PHP version: | 4.0.1+ |
PHP String Reference