function gainChinese($str='',$length=99999999999)
{
$count = 0;
$str_count = strlen($str);
$result = '';
if ($str_count>0) {
for($i=0;$i<$str_count;$i++){
$str_i = mb_substr($str,$i,1,'utf-8');
if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str_i) && $count<$length){
$result .= $str_i;
$count ++;
}
}
}
return $result;
}
$content = "阿斯达岁的嗯我热无热无问213二七万达所多若群二无若asdasdqweewrqwdas请问苏打水";
$data = gainChinese($content);
$ret = [];
for ($i=0;$i<9999;$i++){
$Chinese_character = mb_substr($data, $i, 1, 'utf-8');
if(empty($Chinese_character)) break;
if(!isset($ret[$Chinese_character])){
$ret[$Chinese_character] = 1;
}else{
$ret[$Chinese_character] += 1;
}
}
asort($ret);
var_dump($ret);
输出结果:
[
'阿' => 1
'七' => 1
'打' => 1
'苏' => 1
'请' => 1
'群' => 1
'多' => 1
'所' => 1
'万' => 1
'水' => 1
'斯' => 1
'我' => 1
'嗯' => 1
'的' => 1
'岁' => 1
'问' => 2
'热' => 2
'若' => 2
'达' => 2
'二' => 2
'无' => 3
]
|