Tuoboo's blog
Current Page: Home > Web Programming >

How to using Php compare strings function: similar_text()

How to using Php compare strings function: similar_text()
The webmaster of information publishing website most to meet a lot of users to post the same posts, to increase the exposure of its own information, and as a site administrator, in addition to the use of cookies, IP restrictions and other technology, we can also take advantage of PHP's band similar_text function to determine the user posted content similarity.

similar_text()
(PHP 4, PHP 5)

int similar_text ( string $first , string $second [, float &$percent ] )

Similar_text — calculates the similarity between two strings as described in Oliver [1993]. Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of the longest string.

Let's look code below:

<?php
require('conn.php');

$sql="select title from content order by id desc limit 20"; //calculates the similarity
$result=mysql_query($sql,$conn);
$cf=0;
while($row=mysql_fetch_array($result)){
similar_text($row['title'], $title, $percent); //save as $percent
if($percent>90){$cf=1;break;} //Tuoboo.com: compare the similarity
}

if($cf==1){
echo "<SCRIPT language=JavaScript>alert('Sorry, you are forbidden.');";
echo "this.location.href='vbscript:history.back()';</SCRIPT>";
mysql_close();
exit();
}
?>


This code is useful where the title field to expand into other fields, such as content field, and the PHP functions are too powerful.
Tag: Php, compare strings function, similar_text()
Author: Tuoboo. Time: 10-05-12 11:43
  • 1 Bob says:
    May 13th, 2010 22:29
    This similar_text function is useful, and i konw how to use it, thanks.
*Name
Email (will not be published)
Website
(*Verification Code, Click to refresh.)