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.
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.
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 :
Number of similar characters : 5 Percentage of similar characters : 58.823529411765>
Program 2 : This program will highlight the case-sensitivity of the function.
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.
Number of similar characters : 2 Number of similar characters : 3
- D3.js | d3.max() function
- p5.js | hex() function
- PHP | pi( ) Function
- p5.js | str() function
- PHP | Ds\Map map() Function
- p5.js | arc() Function
- PHP | each() Function
- p5.js | nf() Function
- p5.js | box() Function
- PHP | Ds\Map last() Function
- PHP | pow( ) Function
- PHP | Ds\Map first() Function
- PHP Ds\Map sum() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.