Method 1:
Use regular expression /^[a-z] $/
or /^[A-Z] $/
Make a judgment.
Examples are as follows:
function checkcase($str) { if(preg_match('/^[a-z]+$/', $str)) { echo '小写字母'; } elseif(preg_match('/^[A-Z]+$/', $str)) { echo '大写字母'; } }
Related learning video sharing: php video tutorial
Method 2:
Use functions "ord()
" and "strtoupper()
" are used for judgment.
ord()
The function returns the ASCII value of the first character in the string.
strtoupper()
Function converts a string to uppercase.
Examples are as follows:
<?php $str = 'a'; function checkcase1($str){ $str =ord($str); if($str>64&&$str<91){ echo '大写字母'; return; } if($str>96&&$str<123){ echo '小写字母'; return; } echo'不是字母'; } function checkcase2($str){ if(strtoupper($str)===$str){ echo '大写字母'; }else{ echo '小写字母'; } } echo checkcase1($str); echo checkcase2($str); ?>
Recommended related articles and tutorials: php tutorial
The above is the detailed content of How to determine whether a letter is uppercase or lowercase in php. For more information, please follow other related articles on the PHP Chinese website!