Check for all caps and adjust to proper format?

Is there an easy way to check for all caps using PHP in a form field and adjust it properly?

So this: CAPITALS
will become this: Capitals

Thank you.

Well, if that’s all your after, you could…


<?php
echo ucwords(strtolower($string));
?>

However, if you want to explicitly check for all capitals, you could…


<?php
if(strtoupper($string) === $string){
    #all caps
}
?>

Either of these 3 functions would check for the existence of a capital too.


<?php
function has_a_cap($string){
    return 0 !== strcmp($string, strtolower($string));
}
?>


<?php
function has_a_cap($string){
    foreach(str_split($string) as $chr){
        if(true === in_array(ord($chr), range(65, 90))){
            return true;
        }
    }
    return false;
}
?>


<?php
function has_a_cap($string){
    return count(
        array_diff(
            str_split($string),
            str_split(strtolower($string))
        )
    ) > 0;
}
?>

Off Topic:

Slow night! :slight_smile:

Hehe, awesome, Thank you I will try this. How about check for all letters or all numbers like a rule on a city form field or zip code? City would require all letters and zip would require all numbers?

Thanks.


<?php
function is_all_alpha($string){
    return 0 === preg_match('~[^a-z]~i', $string);
}

function is_all_numeric($string){
    return 0 === preg_match('~[^0-9]~', $string);
}
?>

Off Topic:

Anthony, I’ve recently noticed your habit of using what I think is a peculiar ordering of values separated by operators, e.g. false === some_condition as opposed to the more common some_condition === false. Is there any benefit to doing things this way round?

Off Topic:

Hey Sam,

I guess I brought it over from when I used to use just ‘==’ to compare rather than the stricter ‘===’.

It prevented me from accidentally assigning vars when I should be comparing, such as…


<?php
error_reporting(-1);
ini_set('display_errors', true);

if($var = 'foo'){
    
}
?>

which of course would always be true and pretty hard to spot (try it, you get nothing!). Whereas…


if('foo' = $var)
{
}

would bork the parser and throw out an error.

Parse error: syntax error, unexpected '=' 

Additionally, for me, I find it easier to know what I’m comparing too at first glance.


<?php
function foo($string){
    return preg_match('~some-funky-pattern~i', $string, $matches, FLAG | FLAG | FLAG) === 1;
}
?>

Given this example, I have to look to the far most right to see what it should be matching, preg_* was a bad example though. Maybe…


<?php
function is_foo($string){
    return ucwords(str_rot13(strtolower($string))) === 'foo';
}
?>

<?php
function is_foo($string){
    'foo' === ucwords(str_rot13(strtolower($string)));
}
?>

I’ve seen a lot of seasoned Java, C# folk do it too. Check out this.

I actually find it quite difficult to remember and still slip up, but I’m determined to keep to this style. It’s certainly a mixture of merit/style, but peculiar? :stuck_out_tongue:

Awesome, these worked great. Thank you. One more now if you don’t mind. I have a password field I want to require at least one number and at least 6 characters. Is that one as easy?

Thanks again.

Really should make an effort to do this myself … old habits die hard :expressionless: