$str ='我是中国人我是外国人我是韩国人我是美国人我是中国人我是英国人我是中国人我是外国人';
Count_string($str,5);
function Count_string($sstr,$length)
{
$cnt_tmp = 0;
$cnt = 0;
$str = '';
$str_tmp = array();
$str_arr = array();
mb_internal_encoding("gb2312");
$max_length = (mb_strlen($sstr)-$length);
//取得子串集
for($i=0;$i {
$str_tmp[] = mb_substr($sstr, $i, $length);
}
//去除重复子串
$str_tmp = array_unique($str_tmp);
//计算出现次数
foreach($str_tmp as $key=>$value)
{
$cnt_tmp = mb_substr_count($sstr,$value);
if($cnt_tmp>=$cnt)
{
$cnt = $cnt_tmp;
$str_arr[$value] = $cnt;
}
}
//处理出现多重结果
foreach($str_arr as $key=>$value)
{
if($value == $cnt)
{$str .=$key."
";}
}
echo '出现最多的子串是:
'.$str.'
出现次数:'.$cnt;
}