similar_text – PHP String Functions
Syntax :
Description :
similar_text() function calculates the similarity between two strings with values and percentage.
Note: The levenshtein() function is faster than the similar_text() function. However, the similar_text() function will give you a more accurate result.
Parameter :
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string1, Required, String, first string to be compared.
string2, Required, String, second string to be compared.
percentage, Optional, Float, Variable name to store the similarity in percent
[/table]
Note : Swapping the first and second may yield a different result.
Output :
Returns the number of matching chars in both strings.
The number of matching characters is calculated by finding the longest first common substring, and then doing this for the prefixes and the suffixes, recursively. The lengths of all found common substrings are added.
Related articles : htmlentities(), levenshtein() , metaphone() .
similar_text() – PHP Functions Example 1 : Using example with returning similarity percentage.
<?php echo "Similarity : ".similar_text("Hello all","Hello dear",$percent); echo "\nSimilarity percentage : ".$percent; ?>
See below is the output of above code in web browser.
Similarity : 7
Similarity percentage : 73.684210526316
similar_text() – PHP Functions Example 2 : Using example with returning similarity value.
<?php echo "Similarity : ".similar_text("Hello all","Hello dear"); ?>
See below is the output of above code in Web browser.