PHP Similar_text () function

PHP

It can also calculate the percentage similarity of two strings. The function calculates the similarity in percentage by dividing the result by the average of the lengths of the given strings multiplied by 100.Syntax:
similar_text ($string1, $string2, $percent) 
Parameters:This function takes three parameters as shown in the above syntax, of which the first two must be specified and the last optional. All of these parameters are described below:
  • $string1, $string2:these required parameters define the two strings to compare
  • $percent: this parameter is optional. Specifies the name of the variable to store the percentage similarity. By passing a reference as the third argument, the function will calculate the percentage similarity.
Returned value:returns the number of characters that match between two strings.Examples :
Input: $string1 = "code", $string2 = "coders" Output: 4 (80%) Input: $string1 = "hackers", $string2 = "hackathons" Output: 5 (58.823529411765 %) 
The programs below illustrate the Similar_text() function:Program 1: $sim = similar_text ( "hackers" , " hackathons " , $percent );  
// To display the number of characters matched echo "Number of similar characters: $sim" ;  
// To display the percentage of characters that match echo "Percentage of similar characters: $percent" ;  
?>
Exit
Number of similar characters : 5 Percentage of similar characters: 58.823529411765 > 
Program 2:This program will highlight the case sensitivity of the function. $output = similar_text ( "engineer for engineer" , " Geeks for Geeks " , $percent );  
// To display the number of characters matched echo "Number of similar characters: $output" ;  
// To display the percentage of characters that match echo "Percentage of similar characters: $percent" ;  
?>
Output:
Number of similar characters: 13 Percentage of similar characters: 86.666666666667 
Program 3:The order in which the lines are passed is very important. Changing the variables will give a different result. < tbody> $output1 = similar_text ( "with mysql" , "php is best" );  
// To display the number of characters matched echo "Number of similar characters: $output1" ; $output2 = similar_text ( " php is best " , " with mysql " );  
// To display the number of characters matched echo "Number of similar characters: $output2" ;  
?>
Output:
Number of similar characters: 2 Number of similar characters: 3 
Link :
http://php.net/manual/en/function.similar-text.php



Get Solution for free from DataCamp guru