如何检查字母在PHP中是大写还是小写? [英] How to check if letter is upper or lower in 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 "\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';
}
输出:
â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
- Source: 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中是大写还是小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!