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

Php 比较字符串相像度

WBOY
Release: 2016-06-13 13:03:00
Original
856 people have browsed it

Php 比较字符串相似度

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

?

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

?

语法

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

?

?

实例:

?

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

?

结合实际:

?

<?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;
       }
    }
?>
Copy after login
?

?

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

?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template