Friday, February 13, 2015

Capital Letters Counter in PHP

In this article I would like to share with you a sample program that enables the user to count the number of capital letters in a given word or sentence by our user. I wrote a function called count_capital_letters to count the occurrence of capital letters in a given word or sentence. I hope you will find my work useful in your programming assignments or projects in PHP.

 If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360




Sample Output of Our Program

Program Listing

<html>
<title> Capital Letters 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 capital letters
// in a given word or sentence.

function count_capital_letters($string) {
   return strlen(preg_replace("/[^A-Z]/","", $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'>".count_capital_letters($values)."</font> ".
" in a given word or sentence.";
   }
 }
      

if(isset($_POST['clear'])) {
  $values=" "; 
  $results= " ";
  
}

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