Re: high % of capital letters in a string
| From: | Lars Torben Wilson | Date: | Fri, 07 Jun 2002 04:13:47 +0000 |
| Subject: | Re: high % of capital letters in a string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-101074@lists.php.net to get a copy of this message | ||
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
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506