3 Methods in php to count the number of each character in the string and sort it _ PHP Tutorial

Source: Internet
Author: User
In php, three methods are used to count and sort the numbers of each character in a string. Copy the code as follows :? Php this method is purely a back-function, not explained; functioncountStr ($ str) {$ str_arraystr_split ($ str); $ str_arrayarray_count_values ($ str_array); arso The code is as follows:


// This method is purely a back-function and is not explained;
Function countStr ($ str ){
$ Str_array = str_split ($ str );
$ Str_array = array_count_values ($ str_array );
Arsort ($ str_array );
Return $ str_array;
}
// The following is an example;
$ Str = "asdfgfdas323344 ##\ \ $ fdsdfg * $ ** $ 443563536254fas ";
Print_r (countStr ($ str ));
?>
// This method has some data structure ideas, but it is quite understandable :)
Function countStr2 ($ str ){
$ Str_array = str_split ($ str );
$ Result_array = array ();
Foreach ($ str_array as $ value) {// determines whether the character is a new type. If yes, it is set to 1. if not, it is automatically added;
If (! $ Result_array [$ value]) {
$ Result_array [$ value] = 1;
} Else {
$ Result_array [$ value] ++;
}
}
Arsort ($ result_array );
Return $ result_array;
}
$ Str = "asdfgfdas323344 ##\ \ $ fdsdfg * $ ** $ 443563536254fas ";
Var_dump (countStr2 ($ str ))
?>
// This method is purely a poor version of solution 1. First, find the total classes of all characters, and then use the substr_count function one by one.
Function countStr3 ($ str ){
$ Str_array = str_split ($ str );
$ Unique = array_unique ($ str_array );
Foreach ($ unique as $ v ){
$ Result_array [$ v] = substr_count ($ str, $ v );
}
Arsort ($ result_array );
Return $ result_array;
}
$ Str = "asdfgfdas323344 ##\ \ $ fdsdfg * $ ** $ 443563536254fas ";
Var_dump (countStr3 ($ str ));
?>


* The str_split function is used in any method. Therefore, this function is very important ~

The http://www.bkjia.com/PHPjc/325925.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325925.htmlTechArticle code is as follows :? Php // This method is purely a back-function, not explained; function countStr ($ str) {$ str_array = str_split ($ str); $ str_array = array_count_values ($ str_array ); arso...

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.