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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2814880/
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 07:45:38  来源:igfitidea点击:

How to check if letter is upper or lower in PHP?

phpstringutf-8

提问by Tomasz Smykowski

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?

我也有带有变音符号的 UTF-8 文本,并且想检查该文本的第一个字母是大写还是小写。这该怎么做?

采纳答案by mickmackusa

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

我认为,preg_与此处发布的其他解决方案相比,拨打电话是最直接、简洁和可靠的电话。

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

My pattern breakdown:

我的模式分解:

~      # 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

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

请采取通知时ctype_< 'a'失败,这种电池的测试。

Code: (Demo)

代码:(演示

$tests = ['aa', 'Bbbbb', 'éé', 'iou', 'Δδ'];

foreach ($tests as $test) {
    echo "\n{$test}:";
    echo "\n\tPREG:  " , preg_match('~^\p{Lu}~u', $test)      ? 'upper' : 'lower';
    echo "\n\tCTYPE: " , ctype_upper(mb_substr($test, 0, 1))  ? 'upper' : 'lower';
    echo "\n\t< a:   " , mb_substr($test, 0, 1) < 'a'         ? 'upper' : 'lower';

    $chr = mb_substr ($test, 0, 1, "UTF-8");
    echo "\n\tMB:    " , mb_strtoupper($chr, "UTF-8") == $chr ? 'upper' : 'lower';
}

Output:

输出:

aa:
    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.

如果有人需要区分大写字母、小写字母和非字母,请参阅此帖子



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 Lucan handle), you may want to check if the first character has case variants:

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

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

\p{L&} 或 \p{Cased_Letter}:以小写和大写形式存在的字母(Ll、Lu 和 Lt 的组合)。

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

要包含带有SMALL变体的罗马数字(“数字字母”),您可以在必要时将该额外范围添加到模式中。

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

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

Code: (Demo)

代码:(演示

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

回答by Artefacto

function starts_with_upper($str) {
    $chr = mb_substr ($str, 0, 1, "UTF-8");
    return mb_strtolower($chr, "UTF-8") != $chr;
}

Note that mb_substr is necessary to correctly isolate the first character.

请注意, mb_substr 是正确隔离第一个字符所必需的。

回答by Eugen

Use ctype_upperfor check upper case:

使用ctype_upper的支票大写:

$a = array("Word", "word", "wOrd");

foreach($a as $w)
{
    if(ctype_upper($w{0}))
    {
        print $w;
    }
}

回答by Vidar Vestnes

Tried ?

试过?

$str = 'the text to test';
if($str{0} === strtoupper($str{0})) {
   echo 'yepp, its uppercase';
}
else{
   echo 'nope, its not upper case';
}

回答by Haralan Dobrev

As used in Kohana 2 autoloader function:

在 Kohana 2 自动加载器功能中使用:

echo $char < 'a' ? 'uppercase' : 'lowercase';

When a string character is cast to integer it evaluates to its ASCII number. As you know in the ASCII table first there are some control characters and others. Then the uppercase letters from the Latin alphabet. And then the lowercase letters from the Latin alphabet. Thus you can easily check whether the code of a letter is smaller or bigger than the small latin character a.

当一个字符串字符被转换为整数时,它的计算结果是它的 ASCII 数字。如您所知,首先在 ASCII 表中有一些控制字符和其他字符。然后是拉丁字母表中的大写字母。然后是拉丁字母表中的小写字母。因此,您可以轻松检查字母的代码是否比小拉丁字符小或大a

BTW this is around twice as fast than a solution with regular expressions.

顺便说一句,这比使用正则表达式的解决方案快两倍左右。

回答by e2-e4

Note that PHP provides the ctypefamily like ctype_upper.

请注意,PHP 提供了ctype类似ctype_upper的系列。

You have to set the locale correctly via setLocale()first to get it to work with UTF-8.
See the comment on ctype_alphafor instance.

您必须首先通过setLocale()正确设置区域设置才能使其与 UTF-8 一起使用。
例如,请参阅关于ctype_alpha的评论。

Usage:

用法:

if ( ctype_upper( $str[0] )) {
    // deal with 1st char of $str is uppercase
}

回答by Dimmen

I didn't want numbers and others to be an upper char, so I use:

我不希望数字和其他人成为高位字符,所以我使用:

if(preg_match('/[A-Z]$/',$char)==true)
{
   // this must be an upper char
   echo $char
}

回答by Tony

What about just:

怎么样:

if (ucfirst($string) == $string) {dosomething();}

回答by Kver

If you want it in a nice function, I've used this:

如果你想在一个不错的功能中使用它,我已经使用了这个:

function _is_upper ($in_string)
{
    return($in_string === strtoupper($in_string) ? true : false);
}

Then just call..

然后就打电话..

if (_is_upper($mystring))
{
  // Do....
}

回答by Sumith Harshan

if(ctype_upper(&value)){
    echo 'uppercase';
}
else {
    echo 'not upper case';
}