Code in PHP that calculates an unknown length of string which character appears most frequently _php tutorial

Source: Internet
Author: User
The function to use:
Str_split: Splits a string into an array. A similar function explode () function splits a string into arrays. Array_count_values: Used to count the occurrences of all values in the array.
Arsort: Reverses the array and maintains the index relationship.
It is primarily used to sort the associative arrays that are important for the cell order. $str = "asdfgfdas323344##$\ $fdsdfg *$**$*$**$$443563536254fas";//string of any length
Copy CodeThe code is as follows:
$arr =str_split ($STR);
$arr =array_count_values ($arr);
Arsort ($arr);
Print_r ($arr);

Output:
Copy CodeThe code is as follows:
Array
(
[$] = 7
[3] = 6
[*] = 6
[4] = 5
[F] = 5
[s] = 4
[D] = 4
[5] = 3
[A] = 3
[6] = 2
[2] = 2
[G] = 2
[#] = 2
)

The second method:
The function to use:
Array_unique: Removes duplicate values from the array. Substr_count: Calculates the number of occurrences of a substring in a string.
Copy CodeThe code is as follows:
$str = "asdfgfdas323344##$\ $fdsdfg *$**$*$**$$443563536254fas";//string of any length
$arr =str_split ($STR);
$unique =array_unique ($arr);
foreach ($unique as $a) {
$arr 2[$a]=substr_count ($str, $a);
}
Arsort ($arr 2);
Print_r ($arr 2);

http://www.bkjia.com/PHPjc/325755.html www.bkjia.com true http://www.bkjia.com/PHPjc/325755.html techarticle function used: Str_split: Splits a string into an array. A similar function explode () function splits a string into arrays. Array_count_values: Used to count the number of occurrences of all values in an array ...

  • 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.