Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

PHP Program to count the vowels, digits, and spaces in a string

In this tutorial, we will learn the program to count the vowels, digits, and spaces in a string.

Simple PHP Program to count the vowels, digits, and spaces in a string

<html>
<body>
    <div>
	<h1>program for counting vowal,digits.spaces in string</h2>

 
<?php 
    {
		$string="ali is good boy222";
        $vowels = array('a','e','i','o','u');
        $len = strlen($string);
        $num = 0;
        
        for($i=0; $i<$len; $i++){
            if(in_array($string[$i], $vowels))
            {
                $num++;
            }
			echo "$num";
        }
		function countDigits( $string )
{
    return preg_match_all( "/[0-9]/", $string );
	
}
substr_count($string, ' ');

        echo "Number of vowels : <span>". $num."</span>";
		echo "<br>";
		echo "Number of digits :".countDigits($string);
		echo "<br>";
		echo "Number of spaces :" . substr_count($string," ");
		
    }
	?>
	</body>
</html>

Output

112233334555566666Number of vowels : 6
Number of digits :3
Number of spaces :3

Simple PHP Program to count the vowels, digits, and spaces in a string using while loop

<html>
<body>
    
    <form action="" method="post">
        <input type="text" name="string" />
        <input type="submit" />
    </form>
</body>
</html>
 
<?php 
 
    if($_POST)
    {
        $string = strtolower($_POST['string']);
        $vowels = array('a','e','i','o','u');
        $len = strlen($string);
        $num = 0;
        $i=0;
        while( $i<$len){
            if(in_array($string[$i], $vowels))
            {
                $num++;
            }
			 $i++;
		}
			 		function countDigits( $string )
{
    return preg_match_all( "/[0-9]/", $string );
	
}
substr_count($string, ' ');

        echo "Number of vowels : <span>". $num."</span>";
		echo "<br>";
		echo "Number of digits :".countDigits($string);
		echo "<br>";
		echo "Number of spaces :" . substr_count($string," ");
		
    }
	?>

Output

PHP Program to count the vowels, digits, and spaces in a string using while loop

PHP Program to count the vowels, digits, and spaces in a string using while loop

PHP Program to count the vowels, digits, and spaces in a string with the form

<html>
<body>
    
    <form action="" method="post">
        <input type="text" name="string" />
        <input type="submit" />
    </form>
</body>
</html>
 
<?php 
 
    if($_POST)
    {
        $string = strtolower($_POST['string']);
        $vowels = array('a','e','i','o','u');
        $len = strlen($string);
        $num = 0;
        
        for($i=0; $i<$len; $i++){
            if(in_array($string[$i], $vowels))
            {
                $num++;
            }
        }
		function countDigits( $string )
{
    return preg_match_all( "/[0-9]/", $string );
	
}
substr_count($string, ' ');

        echo "Number of vowels : <span>". $num."</span>";
		echo "<br>";
		echo "Number of digits :".countDigits($string);
		echo "<br>";
		echo "Number of spaces :" . substr_count($string," ");
		
    }
	?>

Topic Covered

PHP Program to count the vowels, digits, and spaces in a string.

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.