<bdo id='clGXS'></bdo><ul id='clGXS'></ul>

      <tfoot id='clGXS'></tfoot>
      <i id='clGXS'><tr id='clGXS'><dt id='clGXS'><q id='clGXS'><span id='clGXS'><b id='clGXS'><form id='clGXS'><ins id='clGXS'></ins><ul id='clGXS'></ul><sub id='clGXS'></sub></form><legend id='clGXS'></legend><bdo id='clGXS'><pre id='clGXS'><center id='clGXS'></center></pre></bdo></b><th id='clGXS'></th></span></q></dt></tr></i><div id='clGXS'><tfoot id='clGXS'></tfoot><dl id='clGXS'><fieldset id='clGXS'></fieldset></dl></div>
    1. <small id='clGXS'></small><noframes id='clGXS'>

      <legend id='clGXS'><style id='clGXS'><dir id='clGXS'><q id='clGXS'></q></dir></style></legend>

        PHP中如何判断字母是大写还是小写?

        时间:2023-10-03

        <tfoot id='xCFJZ'></tfoot>

              • <legend id='xCFJZ'><style id='xCFJZ'><dir id='xCFJZ'><q id='xCFJZ'></q></dir></style></legend>
                <i id='xCFJZ'><tr id='xCFJZ'><dt id='xCFJZ'><q id='xCFJZ'><span id='xCFJZ'><b id='xCFJZ'><form id='xCFJZ'><ins id='xCFJZ'></ins><ul id='xCFJZ'></ul><sub id='xCFJZ'></sub></form><legend id='xCFJZ'></legend><bdo id='xCFJZ'><pre id='xCFJZ'><center id='xCFJZ'></center></pre></bdo></b><th id='xCFJZ'></th></span></q></dt></tr></i><div id='xCFJZ'><tfoot id='xCFJZ'></tfoot><dl id='xCFJZ'><fieldset id='xCFJZ'></fieldset></dl></div>
                  <tbody id='xCFJZ'></tbody>
              • <small id='xCFJZ'></small><noframes id='xCFJZ'>

                  <bdo id='xCFJZ'></bdo><ul id='xCFJZ'></ul>
                  本文介绍了PHP中如何判断字母是大写还是小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我也有带有变音符号的 UTF-8 文本,想检查该文本的第一个字母是大写还是小写.如何做到这一点?

                  I have texts in UTF-8 with diacritic characters also, and would like to check if first letter of this text is upper case or lower case. How to do this?

                  推荐答案

                  我认为,与此处发布的其他解决方案相比,进行 preg_ 调用是最直接、简洁和可靠的调用.

                  It is my opinion that making a preg_ call is the most direct, concise, and reliable call versus the other posted solutions here.

                  echo preg_match('~^p{Lu}~u', $string) ? 'upper' : 'lower';
                  

                  我的模式分解:

                  ~      # starting pattern delimiter 
                  ^      #match from the start of the input string
                  p{Lu} #match exactly one uppercase letter (unicode safe)
                  ~      #ending pattern delimiter 
                  u      #enable unicode matching
                  

                  ctype_<时请注意'a' 在这一系列测试中失败了.

                  Please take notice when ctype_ and < 'a' fail with this battery of tests.

                  代码:(演示)

                  $tests = ['âa', 'Bbbbb', 'Éé', 'iou', 'Δδ'];
                  
                  foreach ($tests as $test) {
                      echo "
                  {$test}:";
                      echo "
                  	PREG:  " , preg_match('~^p{Lu}~u', $test)      ? 'upper' : 'lower';
                      echo "
                  	CTYPE: " , ctype_upper(mb_substr($test, 0, 1))  ? 'upper' : 'lower';
                      echo "
                  	< a:   " , mb_substr($test, 0, 1) < 'a'         ? 'upper' : 'lower';
                  
                      $chr = mb_substr ($test, 0, 1, "UTF-8");
                      echo "
                  	MB:    " , mb_strtoupper($chr, "UTF-8") == $chr ? 'upper' : 'lower';
                  }
                  

                  输出:

                  âa:
                      PREG:  lower
                      CTYPE: lower
                      < a:   lower
                      MB:    lower
                  Bbbbb:
                      PREG:  upper
                      CTYPE: upper
                      < a:   upper
                      MB:    upper
                  Éé:               <-- trouble
                      PREG:  upper
                      CTYPE: lower  <-- uh oh
                      < a:   lower  <-- uh oh
                      MB:    upper
                  iou:
                      PREG:  lower
                      CTYPE: lower
                      < a:   lower
                      MB:    lower
                  Δδ:               <-- extended beyond question scope
                      PREG:  upper  <-- still holding up
                      CTYPE: lower
                      < a:   lower
                      MB:    upper  <-- still holding up
                  

                  如果有人需要区分大写字母、小写字母和非字母,请参阅这篇文章.

                  If anyone needs to differentiate between uppercase letters, lowercase letters, and non-letters see this post.

                  这可能把这个问题的范围扩展得太远了,但如果你输入的字符特别松散(它们可能不存在于Lu可以处理的类别中),你可能需要检查一下第一个字符有大小写变体:

                  It may be extending the scope of this question too far, but if your input characters are especially squirrelly (they might not exist in a category that Lu can handle), you may want to check if the first character has case variants:

                  p{L&} 或 p{Cased_Letter}:存在大小写变体的字母(Ll、Lu 和 Lt 的组合).

                  p{L&} or p{Cased_Letter}: a letter that exists in lowercase and uppercase variants (combination of Ll, Lu and Lt).

                  • 来源:https://www.regular-expressions.info/unicode.html
                  • 要包含带有 SMALL 变体的罗马数字(数字字母"),如有必要,您可以将该额外范围添加到模式中.

                    To include Roman Numerals ("Number Letters") with SMALL variants, you can add that extra range to the pattern if necessary.

                    https://www.fileformat.info/info/unicode/category/Nl/list.htm

                    代码:(演示)

                    echo preg_match('~^[p{Lu}x{2160}-x{216F}]~u', $test) ? 'upper' : 'not upper';
                    

                    这篇关于PHP中如何判断字母是大写还是小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用问号替换无效的 UTF-8 字符,mbstring.substitute_c 下一篇:使用 fgetcsv 读取 CSV 文件时出现 UTF-8 问题

                  相关文章

                  最新文章

                  1. <small id='0TcR5'></small><noframes id='0TcR5'>

                      <bdo id='0TcR5'></bdo><ul id='0TcR5'></ul>
                    <legend id='0TcR5'><style id='0TcR5'><dir id='0TcR5'><q id='0TcR5'></q></dir></style></legend>
                    1. <tfoot id='0TcR5'></tfoot>
                      <i id='0TcR5'><tr id='0TcR5'><dt id='0TcR5'><q id='0TcR5'><span id='0TcR5'><b id='0TcR5'><form id='0TcR5'><ins id='0TcR5'></ins><ul id='0TcR5'></ul><sub id='0TcR5'></sub></form><legend id='0TcR5'></legend><bdo id='0TcR5'><pre id='0TcR5'><center id='0TcR5'></center></pre></bdo></b><th id='0TcR5'></th></span></q></dt></tr></i><div id='0TcR5'><tfoot id='0TcR5'></tfoot><dl id='0TcR5'><fieldset id='0TcR5'></fieldset></dl></div>