php函数,用于统计字符串中类似字符的数量


php function that counts number of similar characters in a string

是否有php函数可以统计字符串中类似字符的数量?

我看过levenstein、similar_text和phosphone,似乎没有一个能做到这一点。

输入/输出示例如下:你好,Heil输出3(h,e,l)

使用count_chars

示例

$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
   echo "There were $val instance(s) of '"" , chr($i) , "'" in the string.'n";
}

http://php.net/manual/en/function.count-chars.php

您可以使用str_split将字符串转换为数组,然后使用array_uniquearray_intersect获得常用字母。

例如:

$str1 = "hello";
$str2 = "hola";
$chars1 = array_unique(str_split($str1));
$chars2 = array_unique(str_split($str2));
echo "Common characters: ".count(array_intersect($chars1, $chars2));