2011-09-19 29 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"); 
相關問題