Check if a String is ALL CAPS in PHP

前端 未结 9 2512
温柔的废话
温柔的废话 2020-12-05 22:46

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.

相关标签:
9条回答
  • 2020-12-05 23:20

    Check whether strtoupper($str) == $str

    To handle non-ascii use:

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

    0 讨论(0)
  • 2020-12-05 23:22

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

    if ($str == strtoupper($str))
    {
        $str = ucfirst(strtolower($str));
    }
    
    0 讨论(0)
  • 2020-12-05 23:38

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

     $all_upper = !preg_match("/[a-z]/", $string)
    
    0 讨论(0)
提交回复
热议问题