Directly on the code, the length of the substring can be set itself (for example, 4 consecutive characters or 5 characters).
Copy Code code as follows:
$str = ' I am a Chinese I am a foreigner I am a Korean I am an American I am a Chinese I am a Chinese I am a foreigner ';
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);
Get substring Set
for ($i =0; $i <= $max _length; $i + +)
{
$str _tmp[] = Mb_substr ($sstr, $i, $length);
}
To remove a repeating substring
$str _tmp = Array_unique ($str _tmp);
Count occurrences
foreach ($str _tmp as $key => $value)
{
$cnt _tmp = Mb_substr_count ($sstr, $value);
if ($cnt _tmp>= $cnt)
{
$cnt = $cnt _tmp;
$str _arr[$value] = $cnt;
}
}
Processing multiple results
foreach ($str _arr as $key => $value)
{
if ($value = = $cnt)
{$str. = $key. " <br> ";}
}
The most common substring of echo ' is:<br> '. $str. ' <br> occurrences: '. $cnt;
}