PHP code to get the substring with the most occurrences in a Chinese string

WBOY
Release: 2016-07-25 09:00:10
Original
1014 people have browsed it
  1. /***
  2. **The length of the substring can be customized, such as 4 consecutive characters
  3. **site http://bbs.it-home.org
  4. $str =' I am Chinese I am foreigner I am Korean I am American I am Chinese I am British I am Chinese I am foreigner';
  5. Count_string($str,5);
  6. function Count_string($sstr, $length)
  7. {
  8. $cnt_tmp = 0;
  9. $cnt = 0;
  10. $str = '';
  11. $str_tmp = array();
  12. $str_arr = array();
  13. mb_internal_encoding("gb2312");
  14. $ max_length = (mb_strlen($sstr)-$length);
  15. //Get the substring set
  16. for($i=0;$i<=$max_length;$i++)
  17. {
  18. $str_tmp[] = mb_substr($ sstr, $i, $length);
  19. }
  20. //Remove duplicate substrings
  21. $str_tmp = array_unique($str_tmp);
  22. //Calculate the number of occurrences
  23. foreach($str_tmp as $key=>$value)
  24. {
  25. $cnt_tmp = mb_substr_count($sstr,$value);
  26. if($cnt_tmp>=$cnt)
  27. {
  28. $cnt = $cnt_tmp;
  29. $str_arr[$value] = $cnt;
  30. }
  31. }
  32. //Handle multiple results
  33. foreach($str_arr as $key=>$value)
  34. {
  35. if($value == $cnt)
  36. {$str .=$key."
    ";}
  37. }
  38. echo 'The most frequently occurring substring is:
    '.$str.'
    Number of occurrences:'.$cnt;
  39. }
  40. ?>
Copy code

Yes Interested friends can also refer to this article: php calculates the code for the most frequent characters in a string of unknown length.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!