tags:

views:

128

answers:

3

I have a string that is one of the following forms

ABC  // all caps: 
     // not necessarily "ABC", could be any combination of capital letters
Abc  // first letter capitalized, rest are lowercase
abc  // all lowercase

and I need to distinguish which of these three cases it is... what's the best way to do this? There doesn't seem to be an islower() or isupper() function; I suppose I could make one using strtoupper() or strtolower().

+8  A: 

ctype_lower, ctype_upper

http://www.php.net/manual/en/ref.ctype.php

stereofrog
sweet -- thanks!
Jason S
+3  A: 

ctype_upper() and ctype_lower() do the job.

You could do ucfirst(), uclast(), strtolower(), strtoupper() and compare with original string.

If you want to check if a certain char is uppercase, just use substr() and again compare with original.

For more info: PHP Strings

Jacob Relkin
+4  A: 

Using regular expressions something along the lines of:

if(preg_match('/^[A-Z][a-z]*$/', $str)){
  // uppercase first
}else if(preg_match('/^[a-z]+$/', $str)){
  // all lower
}else if(preg_match('/^[A-Z]+$/', $str)){
  // all upper
}
Question Mark