php中计算未知长度的字符串哪个字符出现的次数最多的代码

前端之家收集整理的这篇文章主要介绍了php中计算未知长度的字符串哪个字符出现的次数最多的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用到的函数:
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);

原文链接:https://www.f2er.com/php/27064.html
字符串字符串字符串次数次数

猜你在找的PHP相关文章