我正在寻找从字符串中获取最频繁和最不频繁字符的最简单,最快捷的方法。我正在使用PHP
process-async
o是最多的,而i,l,e或d是最少的。这只是一个简单的例子
答案 0 :(得分:3)
您可以使用php的count_chars(string,mode)
方法来计算字符串中的字符数。更多关于:count_chars()
<?php
$str = "google is good";
$strArray = count_chars($str,1);
$leastArray = array_keys($strArray, min($strArray));
$mostArray = array_keys($strArray, max($strArray));
echo "Least<br />";
foreach ($leastArray as $key=>$value) {
echo "The character <b>'". chr($value). "'</b><br />";
}
echo "Most<br />";
foreach ($mostArray as $key=>$value) {
echo "The character <b>'". chr($value). "'</b><br />";
}
?>
答案 1 :(得分:1)
$long_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vulputate cursus sem, quis dapibus nunc accumsan eget. Fusce gravida vulputate risus vel ultrices. Morbi rhoncus ultricies tellus, non facilisis lorem ullamcorper id. Ut ultricies ligula quis nulla tristique pellentesque. Maecenas pellentesque, magna id porttitor ullamcorper, eros neque convallis dolor, eu suscipit lorem massa sit amet ligula. Integer nec ante id leo sagittis accumsan. Donec eu maximus libero. Donec vitae enim ut ligula placerat interdum. Aenean consequat, diam mattis euismod tempus, turpis lorem faucibus nulla, sed gravida justo nulla in ex. Ut vel consectetur lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vulputate cursus sem, quis dapibus nunc accumsan eget. Fusce gravida vulputate risus vel ultrices. Morbi rhoncus ultricies tellus, non facilisis lorem ullamcorper id. Ut ultricies ligula quis nulla tristique pellentesque. Maecenas pellentesque, magna id porttitor ullamcorper, eros neque convallis dolor, eu suscipit lorem massa sit amet ligula. Integer nec ante id leo sagittis accumsan. Donec eu maximus libero. Donec vitae enim ut ligula placerat interdum. Aenean consequat, diam mattis euismod tempus, turpis lorem faucibus nulla, sed gravida justo nulla in ex. Ut vel consectetur lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vulputate cursus sem, quis dapibus nunc accumsan eget. Fusce gravida vulputate risus vel ultrices. Morbi rhoncus ultricies tellus, non facilisis lorem ullamcorper id. Ut ultricies ligula quis nulla tristique pellentesque. Maecenas pellentesque, magna id porttitor ullamcorper, eros neque convallis dolor, eu suscipit lorem massa sit amet ligula. Integer nec ante id leo sagittis accumsan. Donec eu maximus libero. Donec vitae enim ut ligula placerat interdum. Aenean consequat, diam mattis euismod tempus, turpis lorem faucibus nulla, sed gravida justo nulla in ex. Ut vel consectetur lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vulputate cursus sem, quis dapibus nunc accumsan eget. Fusce gravida vulputate risus vel ultrices. Morbi rhoncus ultricies tellus, non facilisis lorem ullamcorper id. Ut ultricies ligula quis nulla tristique pellentesque. Maecenas pellentesque, magna id porttitor ullamcorper, eros neque convallis dolor, eu suscipit lorem massa sit amet ligula. Integer nec ante id leo sagittis accumsan. Donec eu maximus libero. Donec vitae enim ut ligula placerat interdum. Aenean consequat, diam mattis euismod tempus, turpis lorem faucibus nulla, sed gravida justo nulla in ex. Ut vel consectetur lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vulputate cursus sem, quis dapibus nunc accumsan eget. Fusce gravida vulputate risus vel ultrices. Morbi rhoncus ultricies tellus, non facilisis lorem ullamcorper id. Ut ultricies ligula quis nulla tristique pellentesque. Maecenas pellentesque, magna id porttitor ullamcorper, eros neque convallis dolor, eu suscipit lorem massa sit amet ligula. Integer nec ante id leo sagittis accumsan. Donec eu maximus libero. Donec vitae enim ut ligula placerat interdum. Aenean consequat, diam mattis euismod tempus, turpis lorem faucibus nulla, sed gravida justo nulla in ex. Ut vel consectetur lorem.";
// set the starting microtime
$debug_start = (float) array_sum(explode(' ',microtime()));
// count the characters of the string
$char_count = count_chars($long_string, 1);
// set the ending microtime
$end = (float) array_sum(explode(' ',microtime()));
// how long did this take?
var_dump($section." processing time: ". sprintf("%.4f", ($end-$debug_start))." seconds");
// returns 0.0000 seconds
// sorts the array from highest to lowest occurence
arsort($char_count);
// dump out the array
var_dump($char_count);
由于count_chars()
在长字符串上返回0.0000秒这一事实,除非您正在使用大量字符串和数据集,否则进一步查看性能更佳的代码将毫无意义。 PHP的count_chars()
非常有效。
arsort
按最高值到最低值为您排序数组。
答案 2 :(得分:1)
下面的代码将为您提供一个数组,其中包含给定字符串中具有最高和最低重复字符的字符:
$string = "google is good";
$ar = str_split(str_replace(' ', '', $string)); //removing white spaces and making array with each element
$c =array_count_values($ar); //counting each elements in the array
print_r(array_keys($c, max($c))); // most repeating
print_r(array_keys($c, min($c))); // least repeating