当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > levenshtein() 函数
The levenshtein() function returns the Levenshtein distance between two strings.
levenshtein()函数的作用是:计算两个字符串的Levenshtein距离。
The Levenshtein distance is the number of characters you have to replace, insert or delete to transform string1 into string2.
Levenshtein distance表示的是:你需要替代、插入以及删除的字符数量。
By default, PHP gives each operation (replace, insert, and delete) equal weight. However, you can define the cost of each operation by setting the optional insert, replace, and delete parameters.
在默认情况下,PHP赋予了每项操作(替代、插入、删除)以相同的功能。然而,你可以通过设置可选参数(插入、替代、删除)来定义每项操作的所指定的成本[cost]。
levenshtein(string1,string2,insert,replace,delete) |
Parameter参数 | Description描述 |
---|---|
string1 | Required. First string to compare 必要参数。定义参与比较的第一个字符串 |
string2 | Required. Second string to compare 必要参数。定义参与比较的第二个字符串 |
insert | Optional. The cost of inserting a character. Default is 1 可选参数。定义插入一个字符的成本[cost]。默认值为1 |
replace | Optional. The cost of replacing a character. Default is 1 可选参数。定义替代一个字符的成本[cost]。默认值为1 |
delete | Optional. The cost of deleting a character. Default is 1 可选参数。定义删除一个字符的成本[cost]。默认值为1 |
Note: The levenshtein() function returns -1 if one of the strings exceeds 255 characters.
注意:如果字符串中的字符总数超过255,levenshtein()函数将返回-1。
Note: The levenshtein() function is not case-sensitive.
注意:levenshtein()函数不区分大小写。
Note: The levenshtein() function is faster than the similar_text() function. However, similar_text() will give you a more accurate result with less modifications needed.
注意:levenshtein()函数的运行速度要快于similar_text()函数。然而,similar_text()函数将会给出相对于levenshtein()函数而言更精确的结果,并且你无需对其进行过多修改。
<?php echo levenshtein("Hello World","ello World"); echo "<br />"; echo levenshtein("Hello World","ello World",10,20,30); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
1 30 |