Hello,

Looking for a solution to count the characters in a string...

Any/all help is appreciated! Thanks in advance!

    
    $string = "This is in completely the wrong folder";
    
    for ($x=0;$x<strlen($string);$x++) {
    
     $unique[$string[$x]]++;
    
    }
    
    echo count ($unique);
    
    

    :p

    Added bonus, if you print_r($unique) it'll tell you how many of each character there are.

      Correction:

      @$unique[$string[$x]]++;

      to prevent warnings

        ... or ...
        [man]count_chars()[/man]

        $result=count_chars($string,1);
        echo count($result);
        

          Originally posted by crimaniak
          Correction:

          @$unique[$string[$x]]++;


          to prevent warnings

          WTF? You write all your code with warnings turned off? Are you mad? Surely that would make debugging a total nightmare?

          That, in my opinion, is awful advice.

            <sigh>

            This is the CODE CRITIQUE forum. Doesn't ANYONE read FAQs?

            <moving to correct forum>

              Originally posted by BuzzLY
              <sigh>

              This is the CODE CRITIQUE forum. Doesn't ANYONE read FAQs?

              <moving to correct forum>

              Urrrrrrrm...... PHP Builder Community Forums > PHP Help > Coding > Count Characters?

                Originally posted by jordy
                Urrrrrrrm...... PHP Builder Community Forums > PHP Help > Coding > Count Characters?

                Its been moved into here now. The comments are from before the move.

                  Check!

                  i didn's say a thing then :rolleyes:

                    hmm... why wouldnt strlen() suffice in this case?

                    At most there's still count_chars(), but the OP didnt seem to want more than just the number of characters.

                      Write a Reply...