php比较相似字符串的方法

yipeiwu_com5年前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程序设计有所帮助。

相关文章

Yii2——使用数据库操作汇总(增删查改、事务)

本文介绍了 Yii2——使用数据库操作汇总(增删查改、事务),具体如下: 对象操作 查询 //1.简单查询 $admin=Admin::model()->findAll...

PHP实现非阻塞模式的方法分析

PHP实现非阻塞模式的方法分析

本文实例讲述了PHP实现非阻塞模式的方法。分享给大家供大家参考,具体如下: 程序非阻塞模式,这里也可以理解成并发。而并发又暂且可以分为网络请求并发 和本地并发 。 先说一下网络请求并发...

php中curl、fsocket、file_get_content三个函数的使用比较

抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简...

关于PHP通用返回值设置方法

遇到一个不错的php代码。记录一下。 在写php代码时,经常会遇到需要返回值的情况,可以统一设置一下返回值的格式。 下面就是一个不错的例子。 配置类Return.conf.php &...

PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题

返回一个交集共有元素的数组(只是数组值得比较)、array_intersect_assoc()函数是将键值和值绑定,一起比较交集部分、array_intersect_key()函数是将两...