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

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

王林
Release: 2023-04-07 21:22:02
Original
6103 people have browsed it

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 '大写字母';
             }
}
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template