Determine repeat characters in a php string

后端 未结 5 1255
星月不相逢
星月不相逢 2021-01-16 23:48

I have found many examples of how to find repeat characters in a string. I believe my requirement is unique.

I have string

$string=aabbbccffffd;
         


        
5条回答
  •  误落风尘
    2021-01-17 00:03

    Here's exactly what your looking for :

     $val)
                if ($max < $val) {
                    $max = $val;
                    $i = 0;
                    unset($letter);
                    $letter[$i++] = chr($key);
                } else if ($max == $val)
                    $letter[$i++] = chr($key);
            if (count($letter) === 1)
                echo 'The character the most repeated is "'.$letter[0].'"';
            else if (count($letter) > 1) {
                echo 'The characters the most repeated are : ';
                $count = count($letter);
                foreach ($letter as $key => $value) {
                    echo '"'.$value.'"';
                    echo ($key === $count - 1) ? '.': ', ';
                }
            }
        } else
            echo 'value passed to '.__FUNCTION__.' can\'t be empty';
    }
    
    $str  = 'ddaabbccccsdfefffffqqqqqqffffdaaa';
    printCharMostRepeated($str);
    

提交回复
热议问题