Friday, October 10, 2025
HomeLanguagesPHP similar_text() Function

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 :




<?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("neveropen for neveropen",
                 "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

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS