-
- $arr=str_split($str);
- $arr=array_count_values($arr);
- arsort($arr);
- print_r($arr);
- ?>
-
Copy code
Output:
Array
(
[$] => 7
[3] => 6
=> 6
[4] => 5
[f] => 5
=> 4
[d] => 4
[5] => 3
[a] => 3
[6] => 2
[2] => 2
[g] => 2
[#] => 2
)
Method 2:
Functions used:
array_unique: Remove duplicate values from the array.
substr_count: Count the number of times a substring appears in a string.
-
- $str="asdfgfdas323344##$$fdsdfg*$**$*$**$$443563536254fas";//Any length string
- $arr=str_split($str );
- $unique=array_unique($arr);
- foreach ($unique as $a){
- $arr2[$a]=substr_count($str, $a);
- }
- arsort($arr2);
- print_r ($arr2);
- ?>
Copy code
|