Home  >  Article  >  Backend Development  >  Php 比较字符串相像度

Php 比较字符串相像度

WBOY
WBOYOriginal
2016-06-13 13:03:00832browse

Php 比较字符串相似度

除了利用cookies、IP限制等技术外,我们可以利用PHP自身带的similar_text函数来判断用户发帖内容的相似度。

?

????similar_text()?函数计算两个字符串的匹配字符的数目,也可以计算两个字符串的相似度(以百分比计)。???

?

语法

similar_text(string1,string2,percent)
参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
percent 可选。规定供存储百分比相似度的变量名。

?

?

实例:

?

<?php
similar_text("Web Design & Development","Low Cost, Custom Web Design",$percent);
echo "Percent: $percent%"; ;
?>
?

?

结合实际:

?

<?php
$query = mysql_query("select * from $table") or die("Query failed");

while ($row = mysql_fetch_array($query)) {
      similar_text(strtoupper($_POST['name']), strtoupper($row['reserved']), $similarity_pst);
      if (number_format($similarity_pst, 0) > 90){
        $too_similar = $row['reserved'];
        print "The name you entered is too similar the reserved name "".$row['reserved'].""";
        break;
       }
    }
?>
?

?

转帖注明出处:http://justcoding.iteye.com/blog/988504

?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn