In PHP, checking whether a string consists solely of uppercase letters is a common task. Fortunately, PHP offers a built-in function called ctype_upper(), which makes it easy to perform this check. In the following, we’ll show how to use ctype_upper() to determine whether a string is entirely made up of uppercase letters.
ctype_upper() is a very practical character classification function in PHP that checks if all characters in a string are uppercase letters. The function prototype is as follows:
bool ctype_upper ( string $text )
Parameter: This function accepts a string parameter $text, which is the string you want to check.
Return Value: It returns true if all characters in the string are uppercase letters; otherwise, it returns false.
It’s important to note that ctype_upper() only checks alphabetic characters. If the string contains non-alphabetic characters, the function will return false.
Suppose we want to check if a user input string consists entirely of uppercase letters. We can use the following code:
<?php
// Test string
$str = "HELLOWORLD";
<p>// Use ctype_upper to check if string is all uppercase<br>
if (ctype_upper($str)) {<br>
echo "The string is all uppercase letters.\n";<br>
} else {<br>
echo "The string is not all uppercase letters.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The string is all uppercase letters.
If the string includes non-alphabetic characters such as digits, symbols, or spaces, ctype_upper() will return false. Here’s an example containing numbers and spaces:
<?php
$str = "HELLO WORLD 123";
<p>// Use ctype_upper to check if string is all uppercase<br>
if (ctype_upper($str)) {<br>
echo "The string is all uppercase letters.\n";<br>
} else {<br>
echo "The string is not all uppercase letters.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The string is not all uppercase letters.
If you want to ignore spaces or special characters and only check whether the alphabetic characters are uppercase, you can first remove non-letter characters using preg_replace():
<?php
$str = "HELLO WORLD 123";
<p>// Remove non-letter characters<br>
$filteredStr = preg_replace('/[^a-zA-Z]/', '', $str);</p>
<p>// Use ctype_upper to check if letters are uppercase<br>
if (ctype_upper($filteredStr)) {<br>
echo "The alphabetic part is all uppercase letters.\n";<br>
} else {<br>
echo "The alphabetic part is not all uppercase letters.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The alphabetic part is all uppercase letters.
In this example, we used the regular expression /[^a-zA-Z]/ to strip out all non-letter characters, then checked the remaining string with ctype_upper().
The ctype_upper() function is a highly efficient and easy-to-use tool in PHP for checking whether a string is composed entirely of uppercase letters. With a simple function call, this task can be completed effortlessly. Keep in mind, however, that it doesn't check non-letter characters—if your string includes digits, symbols, or spaces, the function will return false.
We hope this simple example helps you understand how to use ctype_upper() in PHP to determine whether a string is entirely uppercase. If you need to ignore other characters, regular expressions can be used to preprocess the string accordingly.