在字符串中计算元音字母的简单方法?

5
如果我定义了一个简单的字符串变量,如何以最简单的方式计算并输出字符串中元音字母的数量?
我已经搜索了很多类似的方法,但大部分看起来比必要的要复杂。虽然它们都是可行的,也许这种复杂性是必要的,但我正在寻找最简单的解决方案。
我的字符串变量可能是这样的:
$someString = "This is some text with some more text and even more text."

我只想显示出a、e、i、o、u这五个字母的总数。谢谢您提前。

尽管这个问题可能不会真正影响性能,但你应该先寻找最小的复杂度,然后尽可能简化。实际上,当有可能时,你应该始终考虑如何让处理器少思考。 - Tamer Shlash
11个回答

12

这里有一个简单的解决方案:

<?php
$string = "This is some text with some more text and even more text.";
echo "There are <strong>".preg_match_all('/[aeiou]/i',$string,$matches)." vowels</strong> in the string <strong>".$string."</strong>";
?>

8
为什么不直接?
$Vowels = substr_count($someString, 'a')+substr_count($someString, 'e')+substr_count($someString, 'i')+substr_count($someString, 'o')+substr_count($someString, 'u');

然而,我建议你将其封装在一个函数中,否则每次想要重复使用它时都需要更改变量的名称:

function CountVowels($String) {
    return substr_count($String, 'a')+substr_count($String, 'e')+substr_count($String, 'i')+substr_count($String, 'o')+substr_count($String, 'u');
}

echo CountVowels('This is some text with some more text and even more text.');
//Echos 17

--

2018更新:

这可能会更加简洁,如下所示:

function count_vowels(string $s): int {
  return array_sum(array_map(function ($vowel) use ($s) { 
    return substr_count($s, $vowel); 
  }, ['a', 'e', 'i', 'o', 'u']));
}

count_vowels('This is some text with some more text and even more text.');

然而,正如其他人所指出的那样,这种方法可能不是处理长字符串时最快的,因为它需要对字符串进行5次迭代。


注意:这可能比实际迭代要慢(尽管因为它是预定义函数,所以可能更快)。 - John Kurlak
真的很聪明!但是它的性能比它应该有的要多得多。 - Tamer Shlash
你的代码里忘记加 "i" 了。加上 "i" 后应该是这样的: $Vowels = substr_count($someString, 'a')+substr_count($someString, 'e')+substr_count($someString, 'i')+substr_count($someString, 'o')+substr_count($someString, 'u')+substr_count($someString, 'i'); - Amy
@Amy 不确定你在说什么,我的三个例子中都有 i。你的代码有两个 i,会出错。 - Gricey
哦,抱歉,我可能没有看到那个。 - Amy

3

未经测试但应该可以工作:

$matches = preg_match_all('/[aeiou]/i', $someString, $ignore);

2
我认为这个模式必须在一个字符串中。 - icktoofay

2

以下是另一种方法...

$vowels = array('a', 'e', 'i', 'o', 'u');

$vowelsCounts = array_sum(
                 array_intersect_key(
                  array_count_values(
                   str_split(
                    strtolower($str)
                   )
                  ),
                  array_flip($vowels)
                 )
                );

CodePad

但是,这也可以工作,只要确保不在原始字符串上操作,因为它会改变它。

str_replace($vowels, FALSE, $str, $vowelsCounts);

CodePad.


2

我知道现在有点晚了,但是我已经测试了以上所有答案,当处理大字符串时性能并不好。

我相信这段代码在内存和执行时间上都更优秀。

$counter = 0;
$a = array("a","e","i","o","u");
foreach (count_chars($str, 1) as $k => $v) {
    //echo "There were $v instance(s) of \"" .$k."/". chr($k) . "\" in the string.\n";
    if(in_array(strtolower(chr($k)), $a))
        $counter += $v;
}

1
preg_match('#[aeiou]#i', $someString, $matches);
echo count($matches) - 1, "\n";

类似这样的应该可以工作。我想不到更简单的方法。


0

使用 PHP 函数计算字符串中的元音字母数量。

echo substr_count("£string","a")+substr_count("£string","e")+substr_count("£string","i")+substr_count("£string","o")+substr_count("£string","u");

0

可以按照以下方式完成:

public static function count($string)
{
    preg_match_all('/[aeiou]/i', $string, $matches);
    return count($matches[0]);
}

0
$someString = "This is some text with some more text and even more text.";
echo preg_match_all('/[aouie]/i', $someString, $out);

0

不适用于多字节字符集中的字符串。

function countSpecificChars ($string, $charsOfInterest) {
    $count = 0;
    $len = strlen($string);
    for ($i = 0; $i < $len; $i++) {
        if (in_array($string[$i], $charsOfInterest)) {
            $count++;
        }
    }
    return $count;
}

function countVowels ($string) {
    return countSpecificChars($string, array('a', 'e', 'i', 'o', 'u'));
}

echo countVowels('This is some text with some more text and even more text.');
// echoes '17'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接