用php计算字符串中大写字母的最简单、最短的方法?

发布于 2024-08-07 18:30:02 字数 42 浏览 6 评论 0原文

我正在寻找最短、最简单和最优雅的方法来计算给定字符串中大写字母的数量。

I am looking for the shortest, simplest and most elegant way to count the number of capital letters in a given string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉生梦死 2024-08-14 18:30:02
function count_capitals($s) {
  return mb_strlen(preg_replace('![^A-Z]+!', '', $s));
}
function count_capitals($s) {
  return mb_strlen(preg_replace('![^A-Z]+!', '', $s));
}
榆西 2024-08-14 18:30:02
$str = "AbCdE";

preg_match_all("/[A-Z]/", $str); // 3
$str = "AbCdE";

preg_match_all("/[A-Z]/", $str); // 3
儭儭莪哋寶赑 2024-08-14 18:30:02

George Garchagudashvili 解决方案令人惊叹,但如果小写字母包含变音符号或重音符号,则该解决方案会失败。

因此,我做了一个小修复来改进他的版本,该版本也适用于小写重音字母:

public static function countCapitalLetters($string){

    $lowerCase = mb_strtolower($string);

    return strlen($lowerCase) - similar_text($string, $lowerCase);
}

您可以在turbocommons库中找到此方法和许多其他字符串常见操作:

https://github.com/edertone/TurboCommons/blob/70a9de1 737d8c10e0f6db04f5eab0f9c4cbd454f/TurboCommons -php/src/main/php/utils/StringUtils.php#L373

EDIT 2019

Turbocommons 中统计大写字母的方法已经演变为可以统计大小写的方法任何字符串上的字符。您可以在这里查看:

https://github.com/edertone/TurboCommons/blob/1e230446593b13a272b1d6a2903741598bb11bf2/TurboCommons-Php/src/main/php/utils/StringUtils.php#L391

在此处阅读更多信息:

https://turbocommons.org/en/ blog/2019-10-15/count-capital-letters-in-string-javascript-typescript-php

也可以在这里在线测试:

https://turbocommons.org/en/app/stringutils/count-capital-letters

George Garchagudashvili Solution is amazing, but it fails if the lower case letters contain diacritics or accents.

So I did a small fix to improve his version, that works also with lower case accentuated letters:

public static function countCapitalLetters($string){

    $lowerCase = mb_strtolower($string);

    return strlen($lowerCase) - similar_text($string, $lowerCase);
}

You can find this method and lots of other string common operations at the turbocommons library:

https://github.com/edertone/TurboCommons/blob/70a9de1737d8c10e0f6db04f5eab0f9c4cbd454f/TurboCommons-Php/src/main/php/utils/StringUtils.php#L373

EDIT 2019

The method to count capital letters in turbocommons has evolved to a method that can count upper case and lower case characters on any string. You can check it here:

https://github.com/edertone/TurboCommons/blob/1e230446593b13a272b1d6a2903741598bb11bf2/TurboCommons-Php/src/main/php/utils/StringUtils.php#L391

Read more info here:

https://turbocommons.org/en/blog/2019-10-15/count-capital-letters-in-string-javascript-typescript-php

And it can also be tested online here:

https://turbocommons.org/en/app/stringutils/count-capital-letters

丑丑阿 2024-08-14 18:30:02

我会给出另一个解决方案,也许不优雅,但有帮助:

$mixed_case = "HelLo wOrlD";
$lower_case = strtolower($mixed_case);

$similar = similar_text($mixed_case, $lower_case);

echo strlen($mixed_case) - $similar; // 4

I'd give another solution, maybe not elegant, but helpful:

$mixed_case = "HelLo wOrlD";
$lower_case = strtolower($mixed_case);

$similar = similar_text($mixed_case, $lower_case);

echo strlen($mixed_case) - $similar; // 4
永言不败 2024-08-14 18:30:02

它不是最短的,但可以说是最简单的,因为不需要执行正则表达式。通常我会说这应该更快,因为逻辑和检查很简单,但 PHP 总是让我惊讶,因为与其他东西相比,某些东西的速度有多快和慢。

function capital_letters($s) {
    $u = 0;
    $d = 0;
    $n = strlen($s);

    for ($x=0; $x<$n; $x++) {
        $d = ord($s[$x]);
        if ($d > 64 && $d < 91) {
            $u++;
        }
    }

    return $u;
}

echo 'caps: ' .  capital_letters('HelLo2') . "\n";

It's not the shortest, but it is arguably the simplest as a regex doesn't have to be executed. Normally I'd say this should be faster as the logic and checks are simple, but PHP always surprises me with how fast and slow some things are when compared to others.

function capital_letters($s) {
    $u = 0;
    $d = 0;
    $n = strlen($s);

    for ($x=0; $x<$n; $x++) {
        $d = ord($s[$x]);
        if ($d > 64 && $d < 91) {
            $u++;
        }
    }

    return $u;
}

echo 'caps: ' .  capital_letters('HelLo2') . "\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文