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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7465304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:44:32  来源:igfitidea点击:

Simple way to count the vowels in a string in PHP?

php

提问by Jake

If I define a simple string variable, how would I count and output the number of vowels in the string in the simplest possible way?

如果我定义一个简单的字符串变量,我将如何以最简单的方式计算和输出字符串中的元音数量?

I have searched and found a number of similar ways to do so, but most seem more complex than necessary. They are all functional and maybe the complexity IS necessary, but I am looking for the simplest solution possible.

我已经搜索并找到了许多类似的方法,但大多数似乎比必要的更复杂。它们都是功能性的,也许复杂性是必要的,但我正在寻找最简单的解决方案。

My string variable would be something like:

我的字符串变量类似于:

$someString = "This is some text with some more text and even more text."

I just want to display the total instances of a,e,i,o,u. Thank you in advance.

我只想显示 a,e,i,o,u 的总实例。先感谢您。

回答by Nathan

Here's an easy solution:

这是一个简单的解决方案:

<?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>";
?>

回答by Gricey

Why not just

为什么不只是

$Vowels = substr_count($someString, 'a')+substr_count($someString, 'e')+substr_count($someString, 'i')+substr_count($someString, 'o')+substr_count($someString, 'u');

I would, however, encase it in a function otherwise you would have to change the names of the variables every time you want to reuse it:

但是,我会将它封装在一个函数中,否则每次要重用它时都必须更改变量的名称:

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 Update:

2018 更新:

This could be much neater as

这可能会更整洁,因为

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.');

However, as others pointed out, this may not be the fastest on long strings since it has to iterate through the string 5 times.

然而,正如其他人指出的那样,这可能不是长字符串最快的,因为它必须遍历字符串 5 次。

回答by evan

untested but should work:

未经测试,但应该工作:

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

回答by alex

Here is another way to do it...

这是另一种方法来做到这一点......

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

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

CodePad.

键盘

But, this works too, just make sure you don't operate this on the original string, as it will change it.

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

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

CodePad.

键盘

回答by Johnny Jiang

I know it's quite late, but I've tested all answers above and performance wasn't good when handling big string.

我知道现在已经很晚了,但是我已经测试了上面的所有答案,并且在处理大字符串时性能不佳。

I believe this code is better on memory and execution time

我相信这段代码在内存和执行时间上更好

$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;
}

回答by FtDRbwLXw6

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

Something like this ought to work. I can't think of a simpler method.

像这样的事情应该有效。我想不出更简单的方法。

回答by Vivek parikh

Count the vowel in string using function in php.

使用php中的函数计算字符串中的元音。

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

回答by Tamer Shlash

$someString = "This is some text with some more text and even more text.";

$total = 0;
$vowels = Array('a','e','i','o','u');

for ($i=0;$i<strlen($someString);$i++)
{
    for ($j = 0;$j<5;$j++)
        if ($someString[$i] == $vowels[$j])
        {
            $total++;
            break;
        }
}
echo $total;

回答by galchen

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

回答by Hammerite

Not suited for strings in multibyte character sets.

不适合多字节字符集中的字符串。

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'