Php 获取字符串中最多的字母
我需要PHP函数,可以从字符串中获得最多的字母Php 获取字符串中最多的字母,php,Php,我需要PHP函数,可以从字符串中获得最多的字母 $string = "111010111010001101"; $execute = SomeFunction($string); echo $execute; 输出将是这样的 1 有这样的php函数吗?谢谢你不是很有效而是很简单 max(str_split($string)); 可以,只要您的字符集不是字符串中包含多字节字符的多字节字符集。请参见手册页,了解在这里 $string = "111010111010001101"; //$st
$string = "111010111010001101";
$execute = SomeFunction($string);
echo $execute;
输出将是这样的
1
有这样的php函数吗?谢谢你不是很有效而是很简单
max(str_split($string));
可以,只要您的字符集不是字符串中包含多字节字符的多字节字符集。请参见手册页,了解在这里
$string = "111010111010001101";
//$string = "abaacabdeeeee";
$array_count = array_count_values(str_split($string));
$res = array_keys($array_count, max($array_count));
print_r($res);
要使其成为函数,只需执行以下操作:
function SomeFunction($string){
$array_count = array_count_values(str_split($string));
return array_keys($array_count, max($array_count));
}
print_r(SomeFunction('111010111010001101'));
输出
1 //or e in the commented one I tested
Array
(
[0] => a
[1] => b
)
它的工作原理:
- 将字符串拆分为字符数组
str\u Split
- 计算字符
数组\u计数\u值的出现次数
- 获取具有最大出现次数的键
和array\u key
max
$string = 'ababababcd';
输出
1 //or e in the commented one I tested
Array
(
[0] => a
[1] => b
)
你从来没有提到你想在这种情况下发生什么(平局)。对于您的特殊情况,这甚至可能不是一个问题。但为了完整起见,我不得不提一下。如果不希望以数组形式返回,可以执行以下操作(返回false或第一个元素):
干杯