开发者

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 cou开发者_如何学Pythonld make one using strtoupper() or strtolower().


ctype_lower, ctype_upper

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


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
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜