用到的函数: str_split:把字符串分割到数组中。类似的函数explode() 函数把字符串分割为数组。array_count_values:用于统计数组中所有值出现的次数。 arsort:对数组进行逆向排序并保持索引关系。 主要用于对那些单元顺序很重要的结合数组进行排序。$str="asdfgfdas323344##$$fdsdfg*$$*$$$443563536254fas";//任意长度字符串 <div class="codetitle"><a style="CURSOR: pointer" data="61028" class="copybut" id="copybut61028" onclick="doCopy('code61028')"> 代码如下:<div class="codebody" id="code61028"> $arr=str_split($str); $arr=array_count_values($arr); arsort($arr); print_r($arr); 输出: <div class="codetitle"><a style="CURSOR: pointer" data="52742" class="copybut" id="copybut52742" onclick="doCopy('code52742')"> 代码如下:<div class="codebody" id="code52742"> Array ( [$] => 7 [3] => 6 [] => 6 [4] => 5 [f] => 5 [s] => 4 [d] => 4 [5] => 3 [a] => 3 [6] => 2 [2] => 2 [g] => 2 [#] => 2 )
第二种方法: 用到的函数: array_unique:删除数组中重复的值。substr_count:计算子串在字符串中出现的次数。 <div class="codetitle"><a style="CURSOR: pointer" data="30716" class="copybut" id="copybut30716" onclick="doCopy('code30716')"> 代码如下:<div class="codebody" id="code30716"> $str="asdfgfdas323344##$$fdsdfg$$*$$$443563536254fas";//任意长度字符串 $arr=str_split($str); $unique=array_unique($arr); foreach ($unique as $a){ $arr2[$a]=substr_count($str,$a); } arsort($arr2); print_r($arr2);
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|