PHP | similar_text() Function - GeeksforGeeks

23 Mar, 2018
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 charactersecho "Number of similar characters : $sim\n";  // To display the percentage of matching charactersecho "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 charactersecho "Number of similar characters : $output\n";  // To display the percentage of matching charactersecho "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 charactersecho "Number of similar characters : $output1\n";  $output2 = similar_text( "php is best", "with mysql");  // To display the number of matching charactersecho "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
PHP-string
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?