How to get the most frequent substring _php techniques in Chinese strings

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.