What's the best way to see if a string contains all capital letters? I still want the function to return true if the string also contains symbols or numbers.
-
You might find [`s($str)->isUpperCase()`](https://github.com/delight-im/PHP-Str/blob/ea3e40132e9d4ce27da337dae6286f2478b15f56/src/Str.php#L262) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 28 '16 at 04:05
-
See my solution below - no regex or functions required for this. – Brian Smith Jun 26 '17 at 03:32
9 Answers
Check whether strtoupper($str) == $str
To handle non-ascii use:
mb_strtoupper($str, 'utf-8') == $str

- 44,070
- 10
- 68
- 83
-
6
-
2Except it doesnt work for "normal but non english" characters: more strtoupper('øæåØÆÅabcABC') > øæåØÆÅABCABC mb_convert_case or mb_strtoupper – Alexander Morland Nov 29 '13 at 09:24
-
== didn't work for me I'd rather use if (strtoupper($string) != $string) – DevJ3rry Apr 21 '17 at 11:38

- 11,737
- 7
- 53
- 72
-
1As this answer is getting attention, it is interesting to note that it does not reply correctly to the question, because it does not consider numbers, as requested by the author. – jonathancardoso Nov 24 '12 at 16:43
-
1Should note that Ctype functions are part of `Variable and Type Related Extensions`. Should also note that builtin support for ctype is available since `PHP 4.3.0`. – Robin van Baalen Jan 16 '14 at 20:22
-
ctype_upper will return false if the string contains a number. See my solution below. – Brian Smith Jun 26 '17 at 03:28
-
Unfortunately this incorrect answer has too many upvotes to be deleted. Sadly, it will continue to confuse and misinform future researchers. ...if only it could be deleted somehow. – mickmackusa Sep 29 '19 at 03:08
If you want numbers included (and by "symbols" most everything else), then what you are actually trying to test for is the absence of lowercase letters:
$all_upper = !preg_match("/[a-z]/", $string)

- 144,265
- 20
- 237
- 291
I think you are looking for this function
$myString = "ABCDE";
if (ctype_upper($myString)) // returns true if is fully uppercase
{
echo "the string $myString is fully uppercase";
}
Hope it helps

- 645
- 6
- 15
-
1Should note that Ctype functions are part of `Variable and Type Related Extensions`. Should also note that builtin support for ctype is available since `PHP 4.3.0`. – Robin van Baalen Jan 16 '14 at 20:21
You can use preg_match()
. The regular expression would be /^[^a-z]+$/
.
return preg_match('/^[^a-z]+$/', $string) === 1 ? true : false;
Here is the documentation for preg_match().

- 32,161
- 7
- 75
- 66
PCRE solution:
$all_uppercase = preg_match('#^[A-Z]+$#', $string);
just make sure you don't use 'i' modifier

- 1,760
- 1
- 18
- 27
if(mb_strtoupper($string)===$string)
{
do the required task
}
else
{
some other task
}

- 19,457
- 10
- 47
- 56
-
3You have to supply 'utf-8' as a second argument ie. mb_strtoupper('øæåØÆÅabcABC', 'utf-8'); – Alexander Morland Nov 29 '13 at 09:26
In addition to Alexander Morland's comment on Winston Ewert's answer if you need to deal with utf-8 accented characters you can use the following set of functions:
define('CHARSET', 'utf-8');
function custom_substr($content='',$pos_start=0,$num_char=1){
$substr='';
if(function_exists('mb_substr')){
$substr=mb_substr($content,$pos_start,$num_char,CHARSET);
}
else{
$substr=substr($content,$pos_start,$num_char);
}
return $substr;
}
function custom_str_case($string='', $case='lower'){
$lower = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
"з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
"ь", "э", "ю", "я"
);
$upper = array(
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
"З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
"Ь", "Э", "Ю", "Я"
);
if($case=='lower'){
$string = str_replace($upper, $lower, $string);
}
else{
$string = str_replace($lower, $upper, $string);
}
return $string;
}
function custom_strtolower($string){
return custom_str_case($string,'lower');
}
function custom_strtoupper($string){
return custom_str_case($string,'upper');
}
function custom_ucfirst($string){
$string=custom_strtolower($string);
$first_char=custom_substr($string,0,1);
$rest_char=custom_substr($string,1,custom_strlen($string));
$first_char=custom_strtoupper($first_char);
return $first_char.$rest_char;
}
function is_uppercase($string=''){
$is_uppercase=false;
if($string === custom_strtoupper($string)) {
$is_uppercase=true;
}
return $is_uppercase;
}
function is_ucfirst($string=''){
$first_char=custom_substr($string,0,1);
$is_ucfirst=is_uppercase($first_char);
return $is_ucfirst;
}
Resources:: https://github.com/rafasashi/PHP-Custom-String-Functions

- 16,483
- 8
- 84
- 94
This is how I handle ALL CAPS text for my comments section.
if ($str == strtoupper($str))
{
$str = ucfirst(strtolower($str));
}

- 1,443
- 5
- 18
- 24