title tags
countVowels
string,regexp,beginner

Returns number of vowels in the provided string.

  • Use a regular expression to count the number of vowels (aeio and ua) in a string.
function countVowels($string)
{
  preg_match_all('/[aeiou]/i', $string, $matches);

  return count($matches[0]);
}
countVowels('sampleInput'); // 4

Tags: PHP, PHP function, Strings, Vowels, Number of Vowels