Jake Jake - 3 months ago 17
PHP Question

Simple way to count the vowels in a string in PHP?

If I define a simple string variable, how would I count and output the number of vowels in the string in the simplest possible way?

I have searched and found a number of similar ways to do so, but most seem more complex than necessary. They are all functional and maybe the complexity IS necessary, but I am looking for the simplest solution possible.

My string variable would be something like:

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


I just want to display the total instances of a,e,i,o,u. Thank you in advance.

Answer

Here's an easy solution:

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