PHP:计算字符串中的大写单词

时间:2020-11-27 21:36:28

is there an easy way to count uppercase words within a string?

有没有一种简单的方法来计算字符串中的大写单词?

6 个解决方案

#1


Shooting from the hip, but this (or something like it) should work:

从臀部拍摄,但这(或类似的东西)应该有效:

function countUppercase($string) {
     return preg_match_all(/\b[A-Z][A-Za-z0-9]+\b/, $string)
}

countUppercase("Hello good Sir"); // 2

#2


You could use a regular expression to find all uppercase words and count them:

您可以使用正则表达式查找所有大写单词并对其进行计数:

echo preg_match_all('/\b[A-Z]+\b/', $str);

The expression \b is a word boundary so it will only match whole uppercase words.

表达式\ b是一个单词边界,因此它只匹配整个大写单词。

#3


<?php
function upper_count($str)
{
    $words = explode(" ", $str);
    $i = 0;

    foreach ($words as $word)
    {
        if (strtoupper($word) === $word)
        {
            $i++;
        }
    }

    return $i;
}

echo upper_count("There ARE two WORDS in upper case in this string.");
?>

Should work.

#4


This would count the number of uppercase within a string, even for a string that include non-alphanumeric characters

这将计算字符串中的大写数,即使对于包含非字母数字字符的字符串也是如此

function countUppercase($str){ 
     preg_match_all("/[A-Z]/",$str,$matches); 
     return count($matches[0]);
}

#5


A simple solution would be to strip all non-uppercase letters with preg_replace and then count the return string with strlen like so:

一个简单的解决方案是使用preg_replace去除所有非大写字母,然后使用strlen计算返回字符串,如下所示:

function countUppercase($string) {
    echo strlen(preg_replace("/[^A-Z]/","", $string));
}

echo countUppercase("Hello and Good Day"); // 3

#6


$str = <<<A
ONE two THREE four five Six SEVEN eighT
A;
$count=0;
$s = explode(" ",$str);
foreach ($s as $k){
    if( strtoupper($k) === $k){
        $count+=1;
    }
}

#1


Shooting from the hip, but this (or something like it) should work:

从臀部拍摄,但这(或类似的东西)应该有效:

function countUppercase($string) {
     return preg_match_all(/\b[A-Z][A-Za-z0-9]+\b/, $string)
}

countUppercase("Hello good Sir"); // 2

#2


You could use a regular expression to find all uppercase words and count them:

您可以使用正则表达式查找所有大写单词并对其进行计数:

echo preg_match_all('/\b[A-Z]+\b/', $str);

The expression \b is a word boundary so it will only match whole uppercase words.

表达式\ b是一个单词边界,因此它只匹配整个大写单词。

#3


<?php
function upper_count($str)
{
    $words = explode(" ", $str);
    $i = 0;

    foreach ($words as $word)
    {
        if (strtoupper($word) === $word)
        {
            $i++;
        }
    }

    return $i;
}

echo upper_count("There ARE two WORDS in upper case in this string.");
?>

Should work.

#4


This would count the number of uppercase within a string, even for a string that include non-alphanumeric characters

这将计算字符串中的大写数,即使对于包含非字母数字字符的字符串也是如此

function countUppercase($str){ 
     preg_match_all("/[A-Z]/",$str,$matches); 
     return count($matches[0]);
}

#5


A simple solution would be to strip all non-uppercase letters with preg_replace and then count the return string with strlen like so:

一个简单的解决方案是使用preg_replace去除所有非大写字母,然后使用strlen计算返回字符串,如下所示:

function countUppercase($string) {
    echo strlen(preg_replace("/[^A-Z]/","", $string));
}

echo countUppercase("Hello and Good Day"); // 3

#6


$str = <<<A
ONE two THREE four five Six SEVEN eighT
A;
$count=0;
$s = explode(" ",$str);
foreach ($s as $k){
    if( strtoupper($k) === $k){
        $count+=1;
    }
}