2011-09-19 77 views
5

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

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

我的字符串变量会是这样的:

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

我只是想显示的总的情况下,E,I,O,U。先谢谢你。

+0

虽然这个问题可能不会真的影响性能,但是您应该先寻找最小的复杂度,然后尝试尽可能简化。实际上,如果可能的话,您应该始终思考,而不是让处理器思考。 –

回答

8

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

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

未经测试,但应该工作:

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

我认为模式必须在字符串中。 – icktoofay

+0

@icktoofay你是对的 – evan

0
$someString = "This is some text with some more text and even more text."; 
echo preg_match_all('/[aouie]/i', $someString, $out); 
1
preg_match('#[aeiou]#i', $someString, $matches); 
echo count($matches) - 1, "\n"; 

像这样的东西应该工作。我想不出一个更简单的方法。

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 
+0

注意:这可能比实际迭代要慢(尽管这是一个预定义的函数,它可能会更快) –

+0

真的很聪明!但需要更多的性能。 –

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' 
0
$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; 
+0

我想你会想让'继续2'。 – alex

+0

@alex:哦,不!它不应该继续下去,而是应该立刻终止整个第二个循环。 –

+0

“break”和“continue 2”应该做同样的事情,我想。 – alex

1

这里是另一种方式来做到这一点...

$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

1

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

我相信这个代码是内存和执行时间

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

更好计数在PHP中使用函数在字符串中的元音。

echo substr_count("£string","a")+substr_count("£string","e")+substr_count("£string","i")+substr_count("£string","o")+substr_count("£string","u"); 
相关问题