Determine repeat characters in a php string

后端 未结 5 1197
星月不相逢
星月不相逢 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 :

    <?php
    function printCharMostRepeated($str)
    {
        if (!empty($str))
        {
            $max = 0;
            foreach (count_chars($str, 1) as $key => $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);
    
    0 讨论(0)
  • 2021-01-17 00:14
    <?php
      $word = "abcdefghbi";
    
      for($i=0; $i<strlen($word);$i++){
          for($k=0;$k<strlen($word);$k++){
              if($word[$i] == $word[$k] && $i != $k){
                  echo $word[$k]." is duplicate"; 
                  exit;     
              }
        
          }
      }
      echo "no match found";
    ?>
    
    0 讨论(0)
  • use count-chars()

    http://php.net/manual/en/function.count-chars.php

    and then asort()

    http://php.net/manual/en/function.asort.php

    0 讨论(0)
  • 2021-01-17 00:19
    $data = "aabbbcccffffdz";
    $array = str_split($data);
    $v = array_count_values($array);
    foreach($v as $k => $val){ 
            echo $k.' = '.$val.'<br>';
    }
    
    0 讨论(0)
  • 2021-01-17 00:20

    Based on georg's great point, I would use a regex. This will handle split duplicates like ddaaffffd with array keys dd=>2 and ffffd=>3 but will only show one entry for dd when given ddaadd. To represent both would require a more complex array:

    $string = "ddaabbbccffffda";
    preg_match_all('/(.)\1+/', $string, $matches);
    $result = array_combine($matches[0], array_map('strlen', $matches[0]));
    arsort($result);
    

    If you only need a count of ALL occurrences try:

    $result = array_count_values(str_split($string));
    arsort($result);
    

    Legacy Answers:

    If you don't have split duplicates:

    $string  = 'aabbbccffffd';
    $letters = str_split($string);
    $result  = array_fill_keys($letters, 1);
    $previous = '';
    
    foreach($letters as $letter) {
        if($letter == $previous) {
            $result[$letter]++;
        }
        $previous = $letter;
    }
    arsort($result);
    print_r($result);
    

    Or for a regex approach:

    preg_match_all('/(.)\1+/', $string, $matches);
    $result = array_combine($matches[1], array_map('strlen', $matches[0]));
    arsort($result);
    
    0 讨论(0)
提交回复
热议问题