I also added some form validation to make sure that the user will type something in our text field rather than it leave a black value in our text field. I hope you will find our program useful in your learning PHP programming.
If you have some questions please send me an email at jakerpomperada@yahoo.comand jakerpomperada@gmail.com.
People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.
Thank you very much and Happy Productive Programming.
Sample Output of Our Program
Program Listing
<html>
<title> Vowels and Consonants Counter</title>
<style>
body {
font-size:20px;
font-family:arial;
color:blue;
}
</style>
<?php
error_reporting(0);
$values = $_POST['value'];
if(isset($_POST['check'])) {
if ($values==" ") {
echo "<script>";
echo "alert('It Cannot Be Empty!!! Please enter a sentence.');";
echo "</script>";
$values=" ";
$results=" ";
}
if ($values != " ") {
$total_vowels = 0; $total_consonants=0;
$vowels = Array('a','e','i','o','u','A','E','I','O','U');
$vowels = Array('a','e','i','o','u','A','E','I','O','U');
$consonants = Array('b','c','d','f','g','h','j','k','l','m'
,'n','p','q','r','s','t','v','w','x','y','z'
,'B','C','D','F','G','H','J','K','L','M'
,'N','P','Q','R','S','T','V','W','X','Y','Z');
for ($b=0;$b<strlen($values);$b++)
{
for ($a = 0;$a<10;$a++)
if ($values[$b] == $vowels[$a])
{
$total_vowels++;
break;
}
for ($a = 0;$a<42;$a++)
if ($values[$b] == $consonants[$a])
{
$total_consonants++;
break;
}
}
$results .= "==== REPORT ==== ";
$results .= "<br><br>";
$results .= "The sentence is :=> " .$values."<br><br><br>";
$results .= "Total Consonants :=> " .$total_consonants."<br>";
$results .= "Total Vowels :=> " .$total_vowels."<br>";
}
}
if(isset($_POST['clear'])) {
$values=" ";
$results= " ";
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
<br> <h1 align="center">Vowels and Consonants Counter</h2>
<br>
<form action="" method="post">
Enter a Sentence : <input type="text" name="value" value="<?php echo $values; ?>" autofocus size=50/>
<br><br>
<input type="submit" name="check" value="Find Vowels and Consonants"
title="Click here to know the number of Vowels and Consonants in a given sentence."/>
<input type="submit" name="clear" value="Clear"
title="Click here to clear text box and values on the screen"/>
</form>
<br>
<?php
echo $results;
?>
</body>
</html>
DOWNLOAD SOURCE CODE HERE