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 SEO优化之URL优化方法

复制代码 代码如下:function getSeoVersion($phrase) { return preg_replace('/[^a-z0-9_-]/i', '', strtolo...

PHP实现约瑟夫环问题的方法分析

本文实例讲述了PHP实现约瑟夫环问题的方法。分享给大家供大家参考,具体如下: 一、概述 先来看看网上比较常见的约瑟夫环问题描述:约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以...

PHP附件下载中文名称乱码的解决方法

本文实例讲述了PHP附件下载中文名称乱码的解决方法。分享给大家供大家参考,具体如下: PHP中,如果要下载的文件名称为中文,则会出现文件标题乱码。 此时就需要对标题进行编码,也就是说先进...

PHP框架Laravel的小技巧两则

用 Laravel 作为 PHP 开发框架很久了,但是有些官方文档中没有覆盖到的地方,每隔一段时间又会忘记。最近做了一点简单的整理,顺便记录下来备忘。 1. Route::control...

基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)

1、使用curl实现站外采集 具体请参考我上一篇笔记:https://www.jb51.net/article/46432.htm 2、编码转换首先通过查看源代码找到采集的网站使用的编码...