使用PHP similar text计算两个字符串相似度

在网站开发中,我们经常使用PHP similar text 计算两个字符串相似度;

1,similar_text的用法

如果我想计算"ly89cn"和"ly89"的相似程度,有两种表示方法

代码如下:

这样输出4,因为他们有4个字符相等

代码如下:

这样输出80,$percent代表百分比,说明他们有80%的相似程度

当然也可以比较两个中文字符,比如“王业楼的个人博客”和“王业楼”

代码如下:
博客','王业楼');

这样输出9,表示他们的9个字节数相等

代码如下:
博客','王业楼',$percent); echo $percent;

输出54.545454545455,注意中文字符可能不大准确!

PHP similar_text() 函数

实例

计算两个字符串的相似度,并返回匹配字符的数目:

代码如下:

运行实例

定义和用法

similar_text() 函数计算两个字符串的相似度。

函数也能计算两个字符串的百分比相似度。

注释:levenshtein() 函数比 similar_text() 函数更快。不过,similar_text() 函数通过更少的必需修改次数提供更精确的结果。

语法

代码如下:

ottom: rgb(170,170,170) 1px solid; text-align: left; border-left: rgb(170,170) 1px solid; padding-bottom: 0px; text-transform: none; background-color: rgb(249,249,249); text-indent: 0px; margin: 10px 0px 0px; padding-left: 0px; width: 709px; letter-spacing: normal; padding-right: 0px; border-collapse: collapse; font: 12px Verdana,Arial,宋体; white-space: normal; color: rgb(0,0); border-top: rgb(170,170) 1px solid; border-right: rgb(170,170) 1px solid; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">
ottom: rgb(170,170) 1px solid; padding-bottom: 5px; background-color: rgb(213,213,213); margin: 0px; padding-left: 6px; width: 119px; padding-right: 15px; vertical-align: baseline; border-top: rgb(170,170) 1px solid; padding-top: 5px">参数 ottom: rgb(170,213); margin: 0px; padding-left: 6px; padding-right: 15px; vertical-align: baseline; border-top: rgb(170,170) 1px solid; padding-top: 5px">描述 ottom: rgb(170,170) 1px solid; border-left: rgb(170,170) 1px solid; padding-bottom: 6px; background-color: rgb(239,239,239); margin: 0px; padding-left: 6px; padding-right: 15px; vertical-align: text-top; border-top: rgb(170,170) 1px solid; padding-top: 6px">ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">string1ottom: rgb(170,170) 1px solid; padding-top: 6px">必需。规定要比较的第一个字符串。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">string2ottom: rgb(170,170) 1px solid; padding-top: 6px">必需。规定要比较的第二个字符串。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">percentottom: rgb(170,170) 1px solid; padding-top: 6px">可选。规定供存储百分比相似度的变量名。

技术细节

ottom: rgb(170,170) 1px solid; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">
ottom: rgb(170,239); margin: 0px; padding-left: 6px; width: 119px; padding-right: 15px; vertical-align: text-top; border-top: rgb(170,170) 1px solid; padding-top: 6px">返回值:ottom: rgb(170,170) 1px solid; padding-top: 6px">返回两个字符串的匹配字符的数目。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">PHP 版本:ottom: rgb(170,170) 1px solid; padding-top: 6px">4+

更多实例

例子 1

计算两个字符串之间的百分比相似度:

代码如下:

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...