How to Count Vowels in a String Using PHP?

How to Expose and Route to a Resource?
How to Expose and Route to a Resource?

Problem:- How to Count Vowels in a String Using PHP?

Solution – Count Vowels in a String Using PHP

<?php
$string = 'Hello User! I Am a PHP Script';
$vowels = 0;
for($i = 0, $j = strlen($string) ; $i< $j ; $i++){
	if(strstr('aeiouAEIOU', $string[$i])){
		$vowels++;
	}
}
echo $vowels;
?>

How it Works?

The function strlen() used to total length for the loop and strstr() is used to check the single string is available in main string or not. if yes then we increase $vowels value and finally we will get 8 vowels in $string.

इसे भी पढ़े – Loco App से पैसे कैसे कमायें?

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *