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 <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to