Just before the tech group saw an n-Bell question:
How to find the most occurrences of characters in a string using PHP
My idea is to first set the string to the number of groups, through Array_count_values to get the count of elements, sort, take the topmost one for the maximum number of times, then there may be more than the same maximum number of times, for Loop to find out:
$testStr = ' Rewruo ewjrewm '. Php_eol. ' Hcywer Gfaaf D State of China rew '; $testStr = preg_replace ('/\s/', ' ', $testStr);p reg_match_all ('/./u ', $testStr, $strList); $charCount = array_count_values ($strList [0]); Arsort ($charCount); $maxCount = current ($charCount); foreach ($charCount as $char = = $count) {if ($count < $maxCount) {break;} Echo $char. ' appeared '. $count. ' Times
';}
Reply to discussion (solution)
Since all use Array_count_values, with Max to find the intersection is, do not have to loop
You are arsort, direct echo key ($charCount); Is the most characters that appear.
Since all use Array_count_values, with Max to find the intersection is, do not have to loop
You are arsort, direct echo key ($charCount); Is the most characters that appear.
I loop in order to also lose the other characters that are the most number of times. Only one of them has been exported, according to your practice.
Since all use Array_count_values, with Max to find the intersection is, do not have to loop
You are arsort, direct echo key ($charCount); Is the most characters that appear.
I loop in order to also lose the other characters that are the most number of times. Only one of them has been exported, according to your practice.
You haven't tried, have you? Intersection Ah, Array_intersect, first try to see the results ~
You haven't tried, have you? Intersection Ah, Array_intersect, first try to see the results ~
To find the code, my Code looks for the following results:
R has appeared 5 times
W has appeared 5 times
The country has appeared 5 times
E has appeared 5 times
Because there are 4 characters that are the same number and are the maximum number of times
There's no room for simplification.
$testStr = ' Rewruo ewjrewm '. Php_eol. ' Hcywer Gfaaf D State of China rew '; $testStr = preg_replace ('/\s/', ' ', $testStr);p reg_match_all ('/./u ', $testStr, $strList); $strList = array_count_values ($strList [0]), $r = Array_keys ($strList, $m = max ($strList)), echo join ($t = "occurs $m times
", $r). $t;
R has appeared 5 times
E has appeared 5 times
W has appeared 5 times
The country has appeared 5 times