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

相关文章

PHPMailer安装方法及简单实例

PHPMailer安装方法及简单实例

打开你电脑里的PHP.INI文件,找到如下位置,添加红线部分的内容,路径就是你PHPMailer存放的位置:保存,重启apache. 然后借用readme里的一个例子,稍微改一下就可以用...

PHP使用socket发送HTTP请求的方法

本文实例讲述了PHP使用socket发送HTTP请求的方法。分享给大家供大家参考,具体如下: socket方式: $socket = socket_create(AF_INET, S...

PHP中Date()时间日期函数的使用方法小结

语法 date(format,timestamp)参数 描述 format 必需。规定时间戳的格式。 timestamp 可选。规定时间戳。默认是当前的日期和时间 要找出前一天的时间就是...

PHP运行SVN命令显示某用户的文件更新记录的代码

复制代码 代码如下:<?php$user=trim($_GET['user']);$d=$_GET['date'];if(!$d){ $d=date('Ymd',time...

PHP iconv()函数字符编码转换的问题讲解

在php中iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库;但有时候iconv对于部分数据转码会无缘无故的少一些。比如在转换字符"—"到gb2312时会出错...