简体   繁体   中英

PHP uppercase letter in String

Like the title says, I'm looking for a way to check if a string contains an uppercase letter in it. It is for a password field, and I cannot use regex because we have not learned any of that yet in class.

I tried to use ctype_upper but that only seems to work if every character in the string is uppercase.

Is there a way to check any character in a string, but not using regex?

You can try this:

if (strtolower($string) != $string) {
    echo 'You have uppercase in your string';
} else {
    echo 'You have no uppercase in your string';
}

This checks if the converted string to lowercase is equal to the original string. Hope this helps...

Try this..

Use the strtoupper() function to transform the string into all uppercase characters that's capitalized letters, and then compare the transformed string against the original one to see if they are identical. If they are, then you are pretty sure the original string was also a string consisting of ONLY capital letters

if (strtoupper($string) == $string) {
echo $string.' is all uppercase letters.';}

function isPartUppercase($string) { if(preg_match("/[AZ]/", $string)===0) { return true; } return false; }

The function uses a simple regular expression that tries to find any upper case AZ characters, preg_match() returns the number of instances of the expression it finds in the string, but stops at 1, preg_match_all() returns the count of all instances it finds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM