How to know that all letters in a string are upper case?
You can use strtoupper to convert your string to uppercase. You now know that it is uppercase, and you can check if the original string matches it.
ie.
$str === strtoupper($str)
if(strcmp(strtoupper($str), $str) === 0) {
echo 'is uppercase';
}
Use mb_strtoupper where special character encodings are involved.
Use strcmp for binary-safe string comparison.
You could try making a copy of the string, convert the copy to upper case, and compare it to the original string:
public function isUpperCase ($string) {
return $string === strtoupper($str);
}
OR, a better version (which is multibyte-safe*) would be:
public function mb_isUpperCase ($string) {
$upper = mb_convert_case(
$string,
MB_CASE_UPPER,
mb_detect_encoding(
$string
)
);
return $string === $upper;
}
*Note that mb_detect_encoding can fail, and return false. In a production environment you should either provide a list of possible encodings to mb_detect_encoding, or handle the case where mb_detect_encoding fails.
ctype_upper() might be a possibility if all the characters are letters, and it's an ascii string.
If its a long string and you don't want to make a copy then walk the string and look at each character's ascii value. If it is less than 97 you know it is all uppercase.