What is PHP similar_text() Function?
If you want to find number of matching characters between two strings or the similarity in percentage between the strings, use similar_text() function.
How PHP similar_text() Function Works
To find the “matching characters” between two strings, the function compares characters of the two strings and returns the number of matches (check example 1), even there are repetitive characters (check example 2).
To find the “matching percentage” between two strings, the function uses the following equation-
Similarity Percentage = (Matching Characters x 200) / (String1 length + String2 length).
Check example 3.
Syntax:
similar_text(string1, string2, percent)
Parameters:
The Function has 2 required parameters and 1 optional parameter-
string1 (Required): It specifies the first string to be compared.
string2 (Required): It specifies the second string to be compared.
percent (Optional): It specifies the similarity percentage.
Return Values:
The function returns-
- the number of characters matches between two strings.
- similarity percentage between two strings.
Examples:
Example 1: Text similarity between two strings-
<?php
$string1 = "John";
$string2 = "Johny";
$matching_chars = similar_text($string1, $string2);
echo "Number of matching characters: $matching_chars";
?>
Output:
Number of matching characters: 4
Example 2: Text similarity between repetitive characters-
<?php
$string1 = "doll";
$string2 = "dollar";
$matching_chars = similar_text($string1, $string2);
echo "Number of matching characters: $matching_chars";
?>
Output:
Number of matching characters: 4
Example 3: Text similarity percentage between two strings-
<?php
echo "Number of matching characters: " . similar_text("doc", "docs", $percent);
echo "<br />Similarity percent: $percent";
?>
Output:
Number of matching characters: 3
Similarity percent: 85.714285714286
Example 4: Case sensitive Similarity checking between strings-
<?php
echo "Number of matching characters: " . similar_text("doc", "DOC", $percent);
echo "<br />Similarity percent: $percent";
?>
Output:
Number of matching characters: 0
Similarity percent: 0
Example 5: Order of strings affects the result-
<?php
echo "Similarity number: " . similar_text("doll", "docllar", $percent);
echo "<br />Similarity percent: $percent";
echo "<br />Similarity number: " . similar_text("docllbar", "doll", $percent);
echo "<br /> Similarity percent: $percent";
?>
Output:
Similarity number: 4
Similarity percent: 72.727272727273
Similarity number: 4
Similarity percent: 66.666666666667
Notes on similar_text() Function:
- The function works by using a recursive divide-and-conquer approach.
- The function is case-sensitive, meaning, it consider capital letter “A” and small letter “a” are different characters. Check example 4.
- Order of strings can affects the result because of the recursive nature of the algorithm.
- For longer text comparison, the function takes time. For faster result, you can use levenshtein() function.
- This function works best for small text comparisons.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP similar_text() Function
similar_text() function is one of the built-in string functions. Use this function to find out similarity number or percentage between two strings.