similar_​text – PHP String Functions

Syntax :

similar_​text ( string1, string2, percentage);

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

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.
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.
echo "Similarity : ".similar_text("Hello all","Hello dear");

See below is the output of above code in Web browser.

Similarity : 7


You may also like...