The similar_text Function
The similar_text
function takes two strings for comparison and returns the number of matching characters. The percentage of similarity can be obtained as an optional third parameter. The comparison is case-sensitive.
Syntax
similar_text(string1, string2, [&percent]);
Example
Let's compare two similar strings and get the number of matching characters:
<?php
$res = similar_text('Hello', 'Hallo');
echo $res;
?>
Code execution result:
4
Example
Get the percentage of string matching:
<?php
similar_text('Hello', 'Hallo', $percent);
echo $percent;
?>
Code execution result:
80
Example
Comparing strings with different case:
<?php
similar_text('Hello', 'hello', $percent);
echo $percent;
?>
Code execution result:
80
See Also
-
the
levenshtein
function,
which calculates the Levenshtein distance between strings -
the
soundex
function,
which calculates the soundex key of a string