【问题标题】:Check if a String is ALL CAPS in PHP检查字符串是否在 PHP 中全部大写
【发布时间】:2010-11-18 05:09:56
【问题描述】:

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

【问题讨论】:

标签: php string


【解决方案1】:

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

处理非ASCII使用:

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

【讨论】:

  • 简单又甜美!就像俄罗斯人在太空中使用铅笔的传说一样。
  • 除非它不适用于“普通但非英语”字符:更多 strtoupper('øæåØÆÅabcABC') > øæåØÆÅABCABC mb_convert_case 或 mb_strtoupper
  • == 对我不起作用我宁愿使用 if (strtoupper($string) != $string)
【解决方案2】:

【讨论】:

  • 随着这个答案越来越受到关注,有趣的是它没有正确回答问题,因为它没有按照作者的要求考虑数字。
  • 应该注意Ctype函数是Variable and Type Related Extensions的一部分。还应注意,自 PHP 4.3.0 以来,可提供对 ctype 的内置支持。
  • 如果字符串包含数字,ctype_upper 将返回 false。请参阅下面的解决方案。
  • 不幸的是,这个不正确的答案有太多的赞成票被删除。可悲的是,它将继续混淆和误导未来的研究人员。 ...如果它可以以某种方式被删除。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

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

    这是 preg_match() 的文档。

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

    【讨论】:

      【解决方案5】:

      PCRE 解决方案:

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

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

      【讨论】:

        【解决方案6】:
        if(mb_strtoupper($string)===$string)
        {
          do the required task
        }
        else
        {
          some other task
        }
        

        【讨论】:

        • 您必须提供 'utf-8' 作为第二个参数,即。 mb_strtoupper('øæåØÆÅabcABC', 'utf-8');
        【解决方案7】:

        我想你正在寻找这个功能

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

        希望对你有帮助

        【讨论】:

        • 应该注意Ctype函数是Variable and Type Related Extensions的一部分。还应注意,自 PHP 4.3.0 以来,可提供对 ctype 的内置支持。
        【解决方案8】:

        除了 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", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
                "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
                "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
                "ь", "э", "ю", "я"
            );
            $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", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
                "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
                "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
                "Ь", "Э", "Ю", "Я"
            );
        
            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;
        }
        

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

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            猜你喜欢
            • 2010-10-01
            • 2014-10-10
            • 1970-01-01
            • 2020-04-05
            • 1970-01-01
            • 2014-03-26
            • 2017-12-24
            • 2014-08-13
            相关资源
            最近更新 更多