当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > levenshtein() 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 levenshtein() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 534 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

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]。

Syntax
语法

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


Tips and Notes
提示和注意点

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()函数而言更精确的结果,并且你无需对其进行过多修改。


Example
案例

<?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

评论 (0) All

登陆 | 还没注册?