Wednesday, April 8, 2015

Lowercase and Digits Counter in PHP

In this article I would like to share a program that I wrote that will allows the user to count the number of occurrence of lowercase letters and digits in a given sentence using PHP as our programming language. What does the program will do is to ask the user to enter a word or a sentence and then our program will count how many times the lowercase letters and digits occurs in a given word or sentence.

If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.


Thank you very much and God Bless.




Program Listing

<html>
<title> Lowercase Letters and Digits Counter</title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>
<?php
error_reporting(0);

$values = $_POST['value'];

// function to count the number of lowercase letters
// in a given word or sentence.

function lowercase_letters($string) {
   return strlen(preg_replace("/[^a-z]/","", $string));
}
// function to count the number of digits in a given
// word or sentence.

function count_digits($string) {
  return count(preg_grep('~^[0-9]$~', str_split($string)));
}
  
if(isset($_POST['check'])) {

  if ($values==" ") {
      echo "<script>";  
  echo "alert('It Cannot Be Empty!!! Please enter a number.');";
  echo "</script>";
  $values=" ";
  $results=" ";
     }

 if ($values != " ") {

   $results .=  "==== REPORT ==== ";
   $results .=  "<br><br>";
   $results .= "The sentence or the word is "
            ."<font color='RED'>".$values."</font>"."."
."<br><br>";
   $results .= "The number of capital letters is "
                ."<font color='RED'>".lowercase_letters($values)."</font> ".
" in a given word or sentence."."<br><br>";
$results .= "The number of digits is "
                ."<font color='RED'>".count_digits($values)."</font> ".
" in a given word or sentence.";
   }
 }
      
 
if(isset($_POST['clear'])) {
  $values=" "; 
  $results= " ";
  
}

?>
<body style='background-color:lightgreen'>
<div style='width:900px;margin:auto'>
  <br> <h1 align="center">Lowercase Letters and Digits Counter </h2>
  <br>
<form action="" method="post">
 Enter a Word or a Sentence : <input type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=80/>
 <br><br>
   <input type="submit" name="check" value="Lowercase Letters and Digits Counter" 
  title="Click here to count the number of lowercase letters and digits in a given word or 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>

No comments:

Post a Comment