Copy code The code is as follows:
//This method is purely a memorization of functions and no explanation;
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 still easy to understand:)
function countStr2($str){
$str_array=str_split ($str);
$result_array=array();
foreach($str_array as $value){//Determine whether the character is a new type, if so, set it to 1, if not, set it automatically Add;
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 lame version of solution one. First find the general class of all characters, and then Use the substr_count function to count 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));
?>
*No matter which method is used , all use the str_split function, so this function is very important~
http://www.bkjia.com/PHPjc/325925.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325925.htmlTechArticleCopy the code as follows: ?php //This method is purely a memorization of functions and no explanation; function countStr($str ){ $str_array=str_split($str); $str_array=array_count_values($str_array); arso...
Statement: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