在 php 中测试给定字符的大写或小写类型的最佳方法是什么?

What is best way to test uppercase or lowercase type of a given character in php?

无论当前当地语言如何,检测字符是大写还是小写的理想方法是什么。

有没有更直接的功能?

假设:将内部字符编码设置为 UTF-8 & 本地浏览器会话为 en-US,en;q=0.5 & 已安装多字节字符串扩展。不要使用 ctype_lower 或 ctype_upper.

请参阅下面应该是多字节兼容的测试代码。

$encodingtype = 'utf8';
$charactervalue = mb_ord($character, $encodingtype);

$characterlowercase = mb_strtolower($character, $encodingtype) ;
$characterlowercasevalue = mb_ord(mb_strtolower($character, $encodingtype));

$characteruppercase = mb_strtoupper($character, $encodingtype);
$characteruppercasevalue = mb_ord(mb_strtoupper($character, $encodingtype));



// Diag Info
echo 'Input: ' . $character . "<br />";
echo 'Input Value: ' . $charactervalue = mb_ord($character, $encodingtype) . "<br />" . "<br />";
echo 'Lowercase: ' . $characterlowercase = mb_strtolower($character, $encodingtype) . "<br />";
echo 'Lowercase Value: ' . $characterlowercasevalue = mb_ord(mb_strtolower($character, $encodingtype)) . "<br />" . "<br />";
echo 'Uppercase: ' . $characteruppercase = mb_strtoupper($character, $encodingtype) . "<br />";
echo 'Uppercase Value: ' . $characteruppercasevalue = mb_ord(mb_strtoupper($character, $encodingtype)) . "<br />" . "<br />";
// Diag Info


if ($charactervalue == $characterlowercasevalue and $charactervalue != $characteruppercasevalue){
    $uppercase = 0;
    $lowercase = 1;
    echo 'Is character is lowercase' . "<br />" . "<br />";
}

elseif ($charactervalue == $characteruppercasevalue and $charactervalue != $characterlowercasevalue ){
    $uppercase = 1;
    $lowercase = 0;
    echo 'Character is uppercase' . "<br />" . "<br />";
}

else{
    $uppercase = 0;
    $lowercase = 0;
    echo 'Character is neither lowercase or uppercase' . "<br />" . "<br />";
}

更新:

通过直接比较更改所有 ctype_ 功能...并支持 Cirilic 字符...

我喜欢使用功能:

$str = array("a", "A", "+", "0", 'ǻ', 'Ͱ', "34", 442);
foreach ($str as $val) {
    $replace = ['à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'ą' => 'a', 'å' => 'a', 'ā' => 'a', 'ă' => 'a', 'ǎ' => 'a', 'ǻ' => 'a', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Ą' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ă' => 'A', 'Ǎ' => 'A', 'Ǻ' => 'A', 'ç' => 'c', 'ć' => 'c', 'ĉ' => 'c', 'ċ' => 'c', 'č' => 'c', 'Ç' => 'C', 'Ć' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Č' => 'C', 'ď' => 'd', 'đ' => 'd', 'Ð' => 'D', 'Ď' => 'D', 'Đ' => 'D', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ę' => 'e', 'ē' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ě' => 'e', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ę' => 'E', 'Ē' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ě' => 'E', 'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'Ĝ' => 'G', 'Ğ' => 'G', 'Ġ' => 'G', 'Ģ' => 'G', 'ĥ' => 'h', 'ħ' => 'h', 'Ĥ' => 'H', 'Ħ' => 'H', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ĩ' => 'i', 'ī' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ſ' => 'i', 'ǐ' => 'i', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ĩ' => 'I', 'Ī' => 'I', 'Ĭ' => 'I', 'Į' => 'I', 'İ' => 'I', 'Ǐ' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ł' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'ľ' => 'l', 'ŀ' => 'l', 'Ł' => 'L', 'Ĺ' => 'L', 'Ļ' => 'L', 'Ľ' => 'L', 'Ŀ' => 'L', 'ñ' => 'n', 'ń' => 'n', 'ņ' => 'n', 'ň' => 'n', 'ʼn' => 'n', 'Ñ' => 'N', 'Ń' => 'N', 'Ņ' => 'N', 'Ň' => 'N', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ð' => 'o', 'ø' => 'o', 'ō' => 'o', 'ŏ' => 'o', 'ő' => 'o', 'ơ' => 'o', 'ǒ' => 'o', 'ǿ' => 'o', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ō' => 'O', 'Ŏ' => 'O', 'Ő' => 'O', 'Ơ' => 'O', 'Ǒ' => 'O', 'Ǿ' => 'O', 'ŕ' => 'r', 'ŗ' => 'r', 'ř' => 'r', 'Ŕ' => 'R', 'Ŗ' => 'R', 'Ř' => 'R', 'ś' => 's', 'š' => 's', 'ŝ' => 's', 'ş' => 's', 'Ś' => 'S', 'Š' => 'S', 'Ŝ' => 'S', 'Ş' => 'S', 'ţ' => 't', 'ť' => 't', 'ŧ' => 't', 'Ţ' => 'T', 'Ť' => 'T', 'Ŧ' => 'T', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ũ' => 'u', 'ū' => 'u', 'ŭ' => 'u', 'ů' => 'u', 'ű' => 'u', 'ų' => 'u', 'ư' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ǘ' => 'u', 'ǚ' => 'u', 'ǜ' => 'u', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ũ' => 'U', 'Ū' => 'U', 'Ŭ' => 'U', 'Ů' => 'U', 'Ű' => 'U', 'Ų' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Ǖ' => 'U', 'Ǘ' => 'U', 'Ǚ' => 'U', 'Ǜ' => 'U', 'ŵ' => 'w', 'Ŵ' => 'W', 'ý' => 'y', 'ÿ' => 'y', 'ŷ' => 'y', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ŷ' => 'Y', 'ż' => 'z', 'ź' => 'z', 'ž' => 'z', 'Ż' => 'Z', 'Ź' => 'Z', 'Ž' => 'Z', 'Ǽ' => 'A', 'ǽ' => 'a'];
    $val = str_replace(array_keys($replace), $replace, $val);
    if (strtoupper($val)!==$val) {
        echo "<p class='text-success'>The string <b>$val</b> consists of lower Case letters .</p>";
    } else {
        echo "<p class='text-danger'>The string <b>$val</b> does not consist lower case letters.</p>";
    }
}

但你需要有字典特殊字符;并且不依赖于语言设置...

更新

更短:带有 gateres 字符重音列表:

$str = array("a", "A", "+", "0", 'ǻ', 'Ͱ', "34", 442);
foreach ($str as $val) {
    $charList = ['à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'ą' => 'a', 'å' => 'a', 'ā' => 'a', 'ă' => 'a', 'ǎ' => 'a', 'ǻ' => 'a', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Ą' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ă' => 'A', 'Ǎ' => 'A', 'Ǻ' => 'A', 'ç' => 'c', 'ć' => 'c', 'ĉ' => 'c', 'ċ' => 'c', 'č' => 'c', 'Ç' => 'C', 'Ć' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Č' => 'C', 'ď' => 'd', 'đ' => 'd', 'Ð' => 'D', 'Ď' => 'D', 'Đ' => 'D', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ę' => 'e', 'ē' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ě' => 'e', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ę' => 'E', 'Ē' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ě' => 'E', 'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'Ĝ' => 'G', 'Ğ' => 'G', 'Ġ' => 'G', 'Ģ' => 'G', 'ĥ' => 'h', 'ħ' => 'h', 'Ĥ' => 'H', 'Ħ' => 'H', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ĩ' => 'i', 'ī' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ſ' => 'i', 'ǐ' => 'i', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ĩ' => 'I', 'Ī' => 'I', 'Ĭ' => 'I', 'Į' => 'I', 'İ' => 'I', 'Ǐ' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ł' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'ľ' => 'l', 'ŀ' => 'l', 'Ł' => 'L', 'Ĺ' => 'L', 'Ļ' => 'L', 'Ľ' => 'L', 'Ŀ' => 'L', 'ñ' => 'n', 'ń' => 'n', 'ņ' => 'n', 'ň' => 'n', 'ʼn' => 'n', 'Ñ' => 'N', 'Ń' => 'N', 'Ņ' => 'N', 'Ň' => 'N', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ð' => 'o', 'ø' => 'o', 'ō' => 'o', 'ŏ' => 'o', 'ő' => 'o', 'ơ' => 'o', 'ǒ' => 'o', 'ǿ' => 'o', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ō' => 'O', 'Ŏ' => 'O', 'Ő' => 'O', 'Ơ' => 'O', 'Ǒ' => 'O', 'Ǿ' => 'O', 'ŕ' => 'r', 'ŗ' => 'r', 'ř' => 'r', 'Ŕ' => 'R', 'Ŗ' => 'R', 'Ř' => 'R', 'ś' => 's', 'š' => 's', 'ŝ' => 's', 'ş' => 's', 'Ś' => 'S', 'Š' => 'S', 'Ŝ' => 'S', 'Ş' => 'S', 'ţ' => 't', 'ť' => 't', 'ŧ' => 't', 'Ţ' => 'T', 'Ť' => 'T', 'Ŧ' => 'T', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ũ' => 'u', 'ū' => 'u', 'ŭ' => 'u', 'ů' => 'u', 'ű' => 'u', 'ų' => 'u', 'ư' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ǘ' => 'u', 'ǚ' => 'u', 'ǜ' => 'u', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ũ' => 'U', 'Ū' => 'U', 'Ŭ' => 'U', 'Ů' => 'U', 'Ű' => 'U', 'Ų' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Ǖ' => 'U', 'Ǘ' => 'U', 'Ǚ' => 'U', 'Ǜ' => 'U', 'ŵ' => 'w', 'Ŵ' => 'W', 'ý' => 'y', 'ÿ' => 'y', 'ŷ' => 'y', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ŷ' => 'Y', 'ż' => 'z', 'ź' => 'z', 'ž' => 'z', 'Ż' => 'Z', 'Ź' => 'Z', 'Ž' => 'Z', 'Ǽ' => 'A', 'ǽ' => 'a'];
    $val = strtr($val, $charList);
    if (strtoupper($val)!==$val) {
        echo "<p class='text-success'>The string <b>$val</b> consists of lower Case letters .</p>";
    } else {
        echo "<p class='text-danger'>The string <b>$val</b> does not consist lower case letters.</p>";
    }
}

添加和样式功能:

function GetIfLoweCase($char) {
    $charList = ['à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'ą' => 'a', 'å' => 'a', 'ā' => 'a', 'ă' => 'a', 'ǎ' => 'a', 'ǻ' => 'a', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Ą' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ă' => 'A', 'Ǎ' => 'A', 'Ǻ' => 'A', 'ç' => 'c', 'ć' => 'c', 'ĉ' => 'c', 'ċ' => 'c', 'č' => 'c', 'Ç' => 'C', 'Ć' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Č' => 'C', 'ď' => 'd', 'đ' => 'd', 'Ð' => 'D', 'Ď' => 'D', 'Đ' => 'D', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ę' => 'e', 'ē' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ě' => 'e', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ę' => 'E', 'Ē' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ě' => 'E', 'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'Ĝ' => 'G', 'Ğ' => 'G', 'Ġ' => 'G', 'Ģ' => 'G', 'ĥ' => 'h', 'ħ' => 'h', 'Ĥ' => 'H', 'Ħ' => 'H', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ĩ' => 'i', 'ī' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ſ' => 'i', 'ǐ' => 'i', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ĩ' => 'I', 'Ī' => 'I', 'Ĭ' => 'I', 'Į' => 'I', 'İ' => 'I', 'Ǐ' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ł' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'ľ' => 'l', 'ŀ' => 'l', 'Ł' => 'L', 'Ĺ' => 'L', 'Ļ' => 'L', 'Ľ' => 'L', 'Ŀ' => 'L', 'ñ' => 'n', 'ń' => 'n', 'ņ' => 'n', 'ň' => 'n', 'ʼn' => 'n', 'Ñ' => 'N', 'Ń' => 'N', 'Ņ' => 'N', 'Ň' => 'N', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ð' => 'o', 'ø' => 'o', 'ō' => 'o', 'ŏ' => 'o', 'ő' => 'o', 'ơ' => 'o', 'ǒ' => 'o', 'ǿ' => 'o', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ō' => 'O', 'Ŏ' => 'O', 'Ő' => 'O', 'Ơ' => 'O', 'Ǒ' => 'O', 'Ǿ' => 'O', 'ŕ' => 'r', 'ŗ' => 'r', 'ř' => 'r', 'Ŕ' => 'R', 'Ŗ' => 'R', 'Ř' => 'R', 'ś' => 's', 'š' => 's', 'ŝ' => 's', 'ş' => 's', 'Ś' => 'S', 'Š' => 'S', 'Ŝ' => 'S', 'Ş' => 'S', 'ţ' => 't', 'ť' => 't', 'ŧ' => 't', 'Ţ' => 'T', 'Ť' => 'T', 'Ŧ' => 'T', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ũ' => 'u', 'ū' => 'u', 'ŭ' => 'u', 'ů' => 'u', 'ű' => 'u', 'ų' => 'u', 'ư' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ǘ' => 'u', 'ǚ' => 'u', 'ǜ' => 'u', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ũ' => 'U', 'Ū' => 'U', 'Ŭ' => 'U', 'Ů' => 'U', 'Ű' => 'U', 'Ų' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Ǖ' => 'U', 'Ǘ' => 'U', 'Ǚ' => 'U', 'Ǜ' => 'U', 'ŵ' => 'w', 'Ŵ' => 'W', 'ý' => 'y', 'ÿ' => 'y', 'ŷ' => 'y', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ŷ' => 'Y', 'ż' => 'z', 'ź' => 'z', 'ž' => 'z', 'Ż' => 'Z', 'Ź' => 'Z', 'Ž' => 'Z', 'Ǽ' => 'A', 'ǽ' => 'a'];
    $char      = strtr($char, $charList);
    if (strtoupper($char) !== $char) {
        return true;
    } else {
        return false;
    }
}
$str = array("a", "A", "+", "0", 'ǻ', 'Ͱ', "34", 442);
foreach ($str as $val) {
    if (GetIfLoweCase($val)) {
        echo "<p class='text-success'>The string <b>$val</b> consists of lower Case letters .</p>";
    } else {
        echo "<p class='text-danger'>The string <b>$val</b> does not consist lower case letters.</p>";
    }
}

我觉得最直接的方法是写一个正则表达式模式来确定字符类型。

在下面的代码片段中,我将在第一个捕获组中搜索大写字母(包括 unicode),或在第二个捕获组中搜索小写字母。如果模式不匹配,则该字符不是字母。

正则表达式中 unicode 字母的一个很好的参考:https://regular-expressions.mobi/unicode.html

写两个由管道分隔的捕获组意味着每种类型的字母将被插入输出数组中的不同索引元素中。 [0] 是全字符串匹配(在这种情况下从未使用过,但它的生成是不可避免的)。 [1] 将保留大写匹配项(或者在存在小写匹配项时为空——作为占位元素)。 [2] 将保留小写匹配——只有在存在小写匹配时才会生成它。

因此,我们可以假设匹配数组中的最高键将决定字母的大小写。

如果输入的字符是非字母,preg_match()会return用0的假结果来表示匹配的次数,此时0与查找一起使用以访问 neither.

代码:(Demo) (Pattern Demo)

$lookup = ['neither', 'upper', 'lower'];
$tests = ['A', 'z', '+', '0', 'ǻ', 'Ͱ', null];

foreach ($tests as $test) {
    $index = preg_match('~(\p{Lu})|(\p{Ll})~u', $test, $out) ? array_key_last($out) : 0;
    echo "{$test}: {$lookup[$index]}\n";
}

输出:

A: upper
z: lower
+: neither
0: neither
ǻ: lower
Ͱ: upper
: neither

还未加入 php7.3 的朋友,您可以这样调用 end() then key()

代码:(Demo)

foreach ($tests as $test) {
    if (preg_match('~(\p{Lu})|(\p{Ll})~u', $test, $out)) {
        end($out); // advance pointer to final element
        $index = key($out);
    } else {
        $index = 0;
    }
    echo "{$test}: {$lookup[$index]}\n";
}

我的第一种方法每次测试最少调用一次函数,最多调用两次。通过在 $lookup[] 中编写 preg_ 调用,我的解决方案可以变成单行代码,但我的目标是提高可读性。


p.s。这是我想出的另一种变体。区别在于 preg_match() 总是匹配,因为最后的空 "alternative" (空分支)。

foreach ($tests as $test) {
    preg_match('~(\p{Lu})|(\p{Ll})|~u', $test, $out);
    echo "\n{$test}: " , $lookup[sizeof($out) - 1];
}