Jump to content


Photo

Upper-case char. count


  • Please log in to reply
3 replies to this topic

#1 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 20 June 2006 - 01:48 AM

How would I get the number of upper-case characters in a string and make a percentage out of it (out of 100%)?

Thanks in advance.

---brandon
PHP is my friend (=

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 20 June 2006 - 02:20 AM

Might look at the 8th comment here...

[a href=\"http://us3.php.net/strtoupper\" target=\"_blank\"]http://us3.php.net/strtoupper[/a]

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 20 June 2006 - 02:47 AM

Thanks!

I had to make some minor changes to his function:
    function get_upper_percent($str)
    {
        $len   = 0;
        $upper = 0;

        $strlen = strlen($str);

        for($i = 0; $i <= $strlen; ++$i) 
        {
            if($str[$i] <> strtolower($str[$i]))
            {
                ++$len;
                ++$upper;
            }

            if($str[$i] <> strtoupper($str[$i]))
                ++$len;
        }
            
        $percent = 0;

        if($len > 0)
            $percent = ($upper/$len) * 100;
            
        return substr($percent , 0 , (($percent > 99) ? 3 : 2));
    }
Although it's not correct all of the time.

The string "aaaAAA" comes out as 60% caps with that function.

Any help?

Edit: SOLVED!
PHP is my friend (=

#4 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 20 June 2006 - 03:07 AM

Use this function to do most of the work for you:
[a href=\"http://www.php.net/manual/en/function.count-chars.php\" target=\"_blank\"]http://www.php.net/manual/en/function.count-chars.php[/a]

Then you can use the array_slice() function:
[a href=\"http://www.php.net/manual/en/function.array-slice.php\" target=\"_blank\"]http://www.php.net/manual/en/function.array-slice.php[/a]
$capitalletters = array_slice($charcountarray, ord('A'), 26);
then you can use this function:
[a href=\"http://www.php.net/manual/en/function.array-sum.php\" target=\"_blank\"]http://www.php.net/manual/en/function.array-sum.php[/a]
$totalcaplets = array_sum($capitalletters);
--Use a similiar method to get the sum of the small letters. It is not the sum of the count_chars function, that will count punctuation, tabs, line feeds, spaces, and any control characters as well.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users