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

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 levenshtein() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 530 ::
收藏到网摘: 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

登陆 | 还没注册?