30 seconds of PHP

  Back to Home

countVowels

stringregexpbeginner

Returns number of vowels in the provided string.

Use a regular expression to count the number of vowels (a, e, i, o and ua) in a string.

function countVowels($string)
{
  preg_match_all('/[aeiou]/i', $string, $matches);

  return count($matches[0]);
}