直接上代码,子串的长度可自己设置(比如连续4个字符的或5个字符的)。
代码如下:
$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<=$max_length;$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."
";}
}
原文链接:https://www.f2er.com/php/25748.htmlCount_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<=$max_length;$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;
}