PHP: Check if any part of a string is uppercase

1 min read php

PHP has some great functions for converting whole or parts of strings to upper or lower case, but what if you want to detect if a string already contains upper case characters?

I had this very problem and came up with this basic function to return a boolean value if a string contained an upper case character.

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

The function uses a simple regular expression that tries to find any upper case A-Z 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.

For those interested, I created this function as the ERP application I work on takes titles from a permission table an automatically capitalises each word, the problem is titles such as “uzERP” shouldn’t be converted in this way, as it becomes “Uzerp” and thus doesn’t match our branding, therefore if a title already contains upper case characters it won’t be subject to capitalisation.


Steve correctly pointed out that this function can be simplified whilst maintaining the boolean returning value, so many thanks to him for providing the following changes:

function isPartUppercase($string) {
return (bool) preg_match(/[A-Z]/, $string);