PHP | similar_text() Function


The similar_text() function is a built-in function in PHP. This function calculates the similarity of two strings and returns the number of matching characters in the two strings. The function operates by finding the longest first common sub-string, and repeating this for the prefixes and the suffixes, recursively. The sum of lengths of all the common sub-strings is returned.

It can also calculate the similarity of the two strings in percent. The function calculates the similarity in percent, by dividing the result by the average of the lengths of the given strings times 100.

Syntax :

similar_text( $string1, $string2, $percent)

Parameters: This function accepts three parameters as shown in the above syntax out of which first two must be supplied and last one is optional. All of these parameters are described below:

  • $string1, $string2 : These mandatory parameters specify the two strings to be compared
  • $percent : This parameter is optional. It specifies a variable name for storing the similarity in percent. By passing a reference as third argument, the function will calculate the similarity in percentage.

Return Value : It returns the number of matching characters between the two strings.

Examples:

Input : $string1 = "code", $string2 = "coders"
Output : 4 (80 %)

Input : $string1 = "hackers", $string2 = "hackathons"
Output : 5 (58.823529411765 %)

Below programs illustrate the similar_text() function:

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$sim = similar_text("hackers", "hackathons", $percent);
  
// To display the number of matching characters
echo "Number of similar characters : $sim\n";
  
// To display the percentage of matching characters
echo "Percentage of similar characters : $percent\n";
  
?>

chevron_right


Output

Number of similar characters : 5
Percentage of similar characters : 58.823529411765>

Program 2 : This program will highlight the case-sensitivity of the function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$output = similar_text("geeks for geeks",
                 "Geeks for Geeks"$percent);
  
// To display the number of matching characters
echo "Number of similar characters : $output\n";
  
// To display the percentage of matching characters
echo "Percentage of similar characters : $percent\n";
  
?>

chevron_right


Output:

Number of similar characters : 13
Percentage of similar characters : 86.666666666667

Program 3: The order of passing the strings is very important. Altering the variables will give a different result.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$output1 = similar_text("with mysql", "php is best");
  
// To display the number of matching characters
echo "Number of similar characters : $output1\n";
  
$output2 = similar_text( "php is best", "with mysql");
  
// To display the number of matching characters
echo "Number of similar characters : $output2\n";
  
?>

chevron_right


Output:

Number of similar characters : 2
Number of similar characters : 3

Reference:
http://php.net/manual/en/function.similar-text.php



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.