php比较相似字符串的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php比较相似字符串的方法。分享给大家供大家参考。具体分析如下:

这里通过php的similar_text函数比较两个字符串的相似性。

$word2compare = "stupid";
$words = array(
  'stupid',
  'stu and pid',
  'hello',
  'foobar',
  'stpid',
  'upid',
  'stuuupid',
  'sstuuupiiid',
);
while(list($id, $str) = each($words)){
  similar_text($str, $word2compare, $percent);
  print "Comparing '$word2compare' with '$str': ";
  print round($percent) . "%\n";
}
/*
Results:
Comparing 'stupid' with 'stupid': 100%
Comparing 'stupid' with 'stu and pid': 71%
Comparing 'stupid' with 'hello': 0%
Comparing 'stupid' with 'foobar': 0%
Comparing 'stupid' with 'stpid': 91%
Comparing 'stupid' with 'upid': 80%
Comparing 'stupid' with 'stuuupid': 86%
Comparing 'stupid' with 'sstuuupiiid': 71%
*/

希望本文所述对大家的php程序设计有所帮助。

相关文章

php页面跳转代码 输入网址跳转到你定义的页面

代码很简单,涉及到header函数的使用,代码如下 复制代码 代码如下: <?php if (!empty($_SERVER['HTTPS']) && ('on' == $_SER...

php基础知识:函数基础知识

函数,所有的语言都有,所以这里只说重点: 1>定义:php不需要定义返回值类型。 2>函数名是非大小写敏感的,不过在调用函数的时候,通常使用其在定义时相同的形式。 ...

php SQL防注入代码集合

SQL防注入代码一复制代码 代码如下: <?php /** * 防sql注入 * @author: zhuyubing@gmail.com * */ /** * reject sq...

linux下为php添加iconv模块的方法

./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --w...

Laravel中使用FormRequest进行表单验证方法及问题汇总

在`Laravel`中,每一个请求都会被封装为一个`Request`对象,`Form Request`对象就是包含了额外验证逻辑(以及访问权限控制)的自定义`Request`类。 本文分...