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

时间:2019-05-03 17:54:22

标签: php regex multibyte

一种检测字符是否大写或小写的理想方法,而与当前本地语言无关。

还有更直接的功能吗?

假设:将内部字符编码设置为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 />";
}
  • //测试1 A // //输出->字符为大写
  • // Test 2 z //输出->字符为小写
  • //测试3 + //输出->字符为小写
  • //测试4 0 //输出->字符既不是小写也不大写
  • //测试5ǻ//带有小环且尖锐的拉丁小写字母A //输出->字符为小写
  • //测试6Ͱ希腊大写字母HETA // //输出->字符为大写
  • //测试7''NULL // Output->字符既不是小写也不是大写

2 个答案:

答案 0 :(得分:1)

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

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

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

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

由于这个原因,我们可以假设matchs数组中的最高键将决定字母的大小写。

如果输入字符为非字母,preg_match()将返回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";
}

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


p.s。这是我梦up以求的另一个变化。区别在于preg_match()总是进行匹配,因为最后一个空的“替代”(空分支)。

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

答案 1 :(得分:-1)

更新

通过直接比较更改所有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>";
    }
}