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 style='color:green; font-weight:bold;'>". $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 style='color:green; font-weight:bold;'>". $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 style='color:green; font-weight:bold;'>". $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.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.