检查字符串是否为 PHP 中的全部大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4211875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:18:18  来源:igfitidea点击:

Check if a String is ALL CAPS in PHP

phpstring

提问by Kirk Ouimet

What's the best way to see if a string contains all capital letters? I still want the function to return true if the string also contains symbols or numbers.

查看字符串是否包含所有大写字母的最佳方法是什么?如果字符串还包含符号或数字,我仍然希望函数返回 true。

回答by Winston Ewert

Check whether strtoupper($str) == $str

检查是否 strtoupper($str) == $str

To handle non-ascii use:

要处理非 ascii 使用:

mb_strtoupper($str, 'utf-8') == $str

mb_strtoupper($str, 'utf-8') == $str

回答by mario

If you want numbers included (and by "symbols" most everything else), then what you are actually trying to test for is the absenceof lowercase letters:

如果您希望包含数字(以及“符号”大多数其他所有内容),那么您实际上要测试的是缺少小写字母:

 $all_upper = !preg_match("/[a-z]/", $string)

回答by Bill Criswell

You can use preg_match(). The regular expression would be /^[^a-z]+$/.

您可以使用preg_match(). 正则表达式将是/^[^a-z]+$/.

return preg_match('/^[^a-z]+$/', $string) === 1 ? true : false;

Here is the documentation for preg_match().

这是 preg_match() 的文档。

http://php.net/manual/en/function.preg-match.php

http://php.net/manual/en/function.preg-match.php

回答by iroel

PCRE solution:

PCRE解决方案:

$all_uppercase = preg_match('#^[A-Z]+$#', $string);

just make sure you don't use 'i' modifier

只要确保你不使用'i'修饰符

回答by Kracekumar

if(mb_strtoupper($string)===$string)
{
  do the required task
}
else
{
  some other task
}

回答by Gabi Dj

I think you are looking for this function

我想你正在寻找这个功能

$myString = "ABCDE";
if (ctype_upper($myString)) // returns true if is fully uppercase
{
    echo "the string $myString is fully uppercase";
}

Hope it helps

希望能帮助到你

回答by RafaSashi

In addition to Alexander Morland's comment on Winston Ewert's answer if you need to deal with utf-8 accented characters you can use the following set of functions:

除了 Alexander Morland 对 Winston Ewert 的回答的评论之外,如果您需要处理 utf-8 重音字符,您还可以使用以下函数集:

define('CHARSET', 'utf-8');

function custom_substr($content='',$pos_start=0,$num_char=1){
    $substr='';
    if(function_exists('mb_substr')){
        $substr=mb_substr($content,$pos_start,$num_char,CHARSET);
    }
    else{
        $substr=substr($content,$pos_start,$num_char);
    }
    return $substr;
}
function custom_str_case($string='', $case='lower'){
    $lower = array(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
        "v", "w", "x", "y", "z", "à", "á", "a", "?", "?", "?", "?", "?", "è", "é", "ê", "?", "ì", "í", "?", "?",
        "e", "?", "ò", "ó", "?", "?", "?", "?", "ù", "ú", "?", "ü", "y", "а", "б", "в", "г", "д", "е", "ё", "ж",
        "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
        "ь", "э", "ю", "я"
    );
    $upper = array(
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
        "V", "W", "X", "Y", "Z", "à", "á", "?", "?", "?", "?", "?", "?", "è", "é", "ê", "?", "ì", "í", "?", "?",
        "D", "?", "ò", "ó", "?", "?", "?", "?", "ù", "ú", "?", "ü", "Y", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
        "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
        "Ь", "Э", "Ю", "Я"
    );

    if($case=='lower'){
        $string = str_replace($upper, $lower, $string);
    }
    else{
        $string = str_replace($lower, $upper, $string);
    }

    return $string;
}
function custom_strtolower($string){
    return custom_str_case($string,'lower');
}
function custom_strtoupper($string){
    return custom_str_case($string,'upper');
}

function custom_ucfirst($string){
    $string=custom_strtolower($string);

    $first_char=custom_substr($string,0,1);
    $rest_char=custom_substr($string,1,custom_strlen($string));
    $first_char=custom_strtoupper($first_char);

    return $first_char.$rest_char;
}

function is_uppercase($string=''){
    $is_uppercase=false;

    if($string === custom_strtoupper($string)) {
       $is_uppercase=true;
    }

    return $is_uppercase;
}
function is_ucfirst($string=''){
    $first_char=custom_substr($string,0,1);

    $is_ucfirst=is_uppercase($first_char);

    return $is_ucfirst;
}

Resources:: https://github.com/rafasashi/PHP-Custom-String-Functions

资源:: https://github.com/rafasashi/PHP-Custom-String-Functions

回答by Brian Smith

This is how I handle ALL CAPS text for my comments section.

这就是我处理评论部分所有大写文本的方式。

if ($str == strtoupper($str))
{
    $str = ucfirst(strtolower($str));
}