Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Return the number (count) of vowels in the given string.

We will consider a, e, i, o, and u as vowels for this Kata.

The input string will only consist of lower case letters and/or spaces.




function getCount($str) {
  $vowelsCount = 0;
  $str2 = preg_replace('/(a|e|i|o|u)/', '', $str);
  $vowelsCount = strlen($str) - strlen($str2);
  return $vowelsCount;

後來找到更簡潔的解法,利用 preg_match_all()

function getCount($str) {
  return preg_match_all('/[aeiou]/i',$str);

回到 PHP Kata 列表