Is there a php function that counts the number of similar characters in a string?
I've looked at levenshtein, similar_text, and metaphone none seem to do this.
Example in/output would be: Hello, Heil outputs 3 (h, e, l)
Is there a php function that counts the number of similar characters in a string?
I've looked at levenshtein, similar_text, and metaphone none seem to do this.
Example in/output would be: Hello, Heil outputs 3 (h, e, l)
You can use str_split
to convert the strings into arrays, then array_unique
and array_intersect
to get the common letters.
For example:
$str1 = "hello";
$str2 = "hola";
$chars1 = array_unique(str_split($str1));
$chars2 = array_unique(str_split($str2));
echo "Common characters: ".count(array_intersect($chars1, $chars2));