Re: high % of capital letters in a string
| From: | Justin French | Date: | Fri, 07 Jun 2002 04:24:08 +0000 |
| Subject: | Re: high % of capital letters in a string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-101077@lists.php.net to get a copy of this message | ||
Thanks Lars & Evan... much appreciated!
Justin French
on 07/06/02 2:13 PM, Lars Torben Wilson (torben@php.net) wrote:
> On Thu, 2002-06-06 at 20:20, Justin French wrote:
>> Hi,
>>
>> How would I determine if there was a high % (say > 40%) of capital letters
>> in a string? I'm trying to think of a way of preventing endless use of
>> capital letters in a message board, to stop YELLERS.
>>
>> Justin French
>> --------------------
>> Creative Director
>> http://Indent.com.au
>> --------------------
>
> One I wrote last year sometime:
>
> <?php
> error_reporting(E_ALL);
>
> $strings = array('This is a test with only one cap.',
> 'This One Has Some More',
> 'AND THIS ONE IS SHOUTING');
>
> function get_caps_pct($string)
> {
> $string = preg_replace('/[^a-z]/i', '', $string);
> $non_caps = strlen(preg_replace('/[^A-Z]/', '', $string));
> $caps = strlen(preg_replace('/[A-Z]/', '', $string));
> return $caps ? ($non_caps / $caps) * 100 : 100;
> }
>
> foreach ($strings as $string)
> {
> $pct = (int) get_caps_pct($string);
> echo "Percentage of characters which are CAPITALS: $pct%\n";
> }
> ?>
>
> Output:
>
> Percentage of characters which are CAPITALS: 4%
> Percentage of characters which are CAPITALS: 38%
> Percentage of characters which are CAPITALS: 100%
>
>
> Cheers,
>
> Torben