Jason S
Jason S

Reputation: 189786

best way to determine lower/uppercase in PHP?

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().

Upvotes: 2

Views: 385

Answers (3)

Question Mark
Question Mark

Reputation: 3606

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
}

Upvotes: 4

Jacob Relkin
Jacob Relkin

Reputation: 163288

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

Upvotes: 3

user187291
user187291

Reputation: 53940

ctype_lower, ctype_upper

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

Upvotes: 8

Related Questions