Open In App

PHP similar_text() Function

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report


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 :




<?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";
  
?>


Output

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

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




<?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";
  
?>


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.




<?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";
  
?>


Output:

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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads