Monday, 29 September 2014

similar_text in PHP

 PHP similar_text() function is used to calculate the number of similar characters between two strings .
The PHP similar_text() function can also calculate the similarity of the strings in percent.

Syntax:

similar_text(string1,string2,percent)
string1 : Required. Specifies the first string

string2 : Required. Specifies the second string

percent : Optional. Specifies variable name to store the percentage of similarity between the input strings.

Note: The levenshtein() function is  also used  to calculate  distance between two strings. levenshtein() function is faster than the similar_text() function but the PHP similar_text() gives you a more accurate results.    

Example:

<?php
$input_str1  = "Good Morning";
$input_str2 = "John Good Morning";
echo similar_text($input_str1,$input_str2);
similar_text($input_str1,$input_str2,$percent_var);
echo "<br />".$percent_var;
?>
The output  will be:

12
82.758620689655

0 comments:

Post a Comment