Home  >  Article  >  Backend Development  >  How to determine whether a letter is uppercase or lowercase in php

How to determine whether a letter is uppercase or lowercase in php

王林
王林Original
2019-12-02 11:57:266096browse

How to determine whether a letter is uppercase or lowercase in php

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 = &#39;a&#39;;
function checkcase1($str){
    $str =ord($str);
   if($str>64&&$str<91){
       echo &#39;大写字母&#39;;
       return;
    }
   if($str>96&&$str<123){
       echo &#39;小写字母&#39;;
       return;
    }
    echo&#39;不是字母&#39;;
}
function checkcase2($str){
   if(strtoupper($str)===$str){
       echo &#39;大写字母&#39;;
    }else{
       echo &#39;小写字母&#39;;
    }
}
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn