How to create PHP form and perform numeric into words conversion?


Php form which takes numerical values and performs some manipulations to display the converted result in words. Use of PHP class, function, if-else and many other programming tools can be understood with this example.

First of lets create a file named index.html and write the following block of code there:





oxyzium.blogspot.com – Numeric to Word Convertor


 

Numeric to In words Convertor

Enter the number :

  Blog: oxyzium.blogspot.com

Now it’s time to create PHP file named as convert.php and write the following code there:



/**
* Class Name: convertor
* Use   : To Convert numericcal values or numbers into words
* Date  : 05-09-2012
* Developer : Sudhanshu Ranjan
* Description :
      * function __construct() is called and initialized at the time of object instantiation of the class.
      * value of text field name ‘textfield’ of the form is stored into @var $value
      * @var $value stored value is checked for the type of stored data i.e. number, string, etc.
      * based upon the entered value and if-else conditions contents $value is stored in @var number and @count is used to store the length of number or string
      * after that convert method is executed to produce or generate the output
*/
  class convertor{
      /**
      * @var $inwords : store the converted number in words
      * @var $number : holds the numerical form of the number
      * @var $count : holds the length or digits of the number
      * @var $n : holds the ref. of number
      * @var $rem : is used to hold remainder while calculating modulas of $n i.e number
      * @var $ct : holds the number in form of 2 digits i.e tens
      * @var $ch : holds the number of hundreds
      * @var $cth : holds the number of thousands and ten thousands
      * @var $clh : holda the number of lakhs and ten lakhs
      * @var $cch : holds the number of crores and ten crores
      * @var $nos : is an array containing values 1…20,30,40..100,1000,100000,10000000
      * @var $vals : is an array containing values at same index position as specified by @var $nos in words.
      */
      private $inwords;
      private $number;
      private $count;
      private $n=0; private $rem=0; private $ct=0; private $ch=0;  private $cth=0; private $clh=0; private $cch=0;
      private $nos=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,100000, 10000000);
      private $vals=array(“One”,”Two”,”Three”,”Four”,”Five”,”Six”,”Seven”,”Eight”,”Nine”,”Ten”,
                        “Eleven”, “Twelve”, “Thirteen”, “Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”, “Eighteen”,
                        “Nineteen”, “Twenty”, “Thirty”, “Forty”, “Fifty”, “Sixty”, “Seventy”, “Eighty”,
                        “Ninety”, “Hundred”,”Thousand”, “Lakh”, “Crore”);
      function __construct($value){
                $this->number=$value;
      }
      public function convert(){
          $this->count=strlen($this->number);
          if(($this->countcount>9)){
                        echo “
Valid Numerical Range : 1 – 9 digit. Try Again!!!

“;
                        return 0;
          }
          $num=$this->number;
          for($i=0;$inos);$i++){
          if($num==$this->nos[$i]){
              if(($num==100)||($num==1000)||($num==10000)||($num==100000)||($num==1000000)||($num==10000000)){
                  break;
              }else{
              return $this->vals[$i];
              }
          }
          }
      //number of digits decide the method or function to be called
      if($this->count==2){
          /*
          * used for 2 digit numbers e.g. 21…29,31…39,41….49,…
          * call2(): to assign @var rem and @var ct with the ones and tens place digits
          * tens(): to concatenate and return a proper 2 digit number representation in words
          */
          $this->call2($num);
          $this->tens();
      }
      if($this->count==3){
          /*
          * used for 3 digit numbers e.g. 100…999
          * call3(): to assign @var rem and @var ch with the last two digits as remainder and with values at hundredths place in ch
          * hundred(): to concatenate and return a proper 3 digit number representation in words this method internally calls tens()
          */
          $this->call3($num);
          $this->hundred();
      }
      if($this->count==4){
           /*
           * used for 4 digit numbers e.g. 1000…9999
           * call4(): to assign @var rem and @var cth with the last 3 digits as remainder and with values at thousands place in cth
           * thousand(): to concatenate and return a proper 4 digit number representation in words this method internally calls hundred(), tens()
           */
          $this->call4($num);
          $this->thousand();
      }
      if($this->count==5){
          /*
           * used for 5 digit numbers e.g. 10000…99999
           * call4(): to assign @var rem and @var cth with the last 4 digits as remainder and with values at thousands place in cth
           * tenThousand(): to concatenate and return a proper 5 digit number representation in words this method internally calls thousand(), hundred(), tens()
           */
          $this->call4($num);
          $this->tenThousand();
      }
      if(($this->count==6)||($this->count==7)){
          /*
           * used for 6-7 digit numbers e.g. 100000-0…999999-9
           * call5(): to assign @var rem and @var clh with the last 5 digits as remainder and with values at thousands place in clh
           * lakhs(): to concatenate and return a proper 6-7 digit number representation in words this method internally calls tenThousand(), thousand(), hundred() and tens()
           */
          $this->call5($num);
          $this->lakhs();
      }
      if(($this->count==8)||($this->count==9)){
          /*
           * used for 8-9 digit numbers e.g. 10000000-0…99999999-9
           * call6(): to assign @var rem and @var ch with the last 7 digits as remainder and with values at thousands place in cch
           * crores(): to concatenate and return a proper 8-9 digit number representation in words this method internally calls above mentioned methods from top to bottom
           */
          $this->call6($num);
          $this->crores();
      }
      return $this->inwords;
      }//end of function convert()
   
      function call2($n){
            $this->rem=$n%10;
            $this->ct=floor($n/10);
            $this->ct=$this->ct*10;
        //    echo “REM: “.$this->rem.” CT: “.$this->ct.”
“;
      }
      function call3($n){
            $this->rem=$n%100;
            $this->ch=floor($n/100);
         //    echo “REM: “.$this->rem.” CH: “.$this->ch.”
“;
      }
      function call4($n){
            $this->rem=$n%1000;
            $this->cth=floor($n/1000);
          //  echo “REM: “.$this->rem.” CTH: “.$this->cth.”
“;
      }
      function call5($n){
            $this->rem=$n%100000;
            $this->clh=floor($n/100000);
          //  echo “REM: “.$this->rem.” CLH: “.$this->clh.”
“;
      }
      function call6($n){
            $this->rem=$n%10000000;
            $this->cch=floor($n/10000000);
       //     echo “REM: “.$this->rem.” CCH: “.$this->cch.”
“;
      }
   
      function ones(){
        for($i=0;$inos);$i++){
            if($this->n==$this->nos[$i]){
                 $this->inwords.=$this->vals[$i];
                 break;
            }
        }
      }
      function tens(){
        for($i=0;$inos);$i++){
            if($this->ct==$this->nos[$i]){
                 $this->inwords.=$this->vals[$i];
                 for($j=0;$jnos);$j++){
                     if($this->rem==$this->nos[$j]){
                         $this->inwords.=” “.$this->vals[$j];
                         break;
                     }
                 }
                 break;
            }
        }
      }
      function hundred(){
           for($i=0;$inos);$i++){
            if($this->ch==$this->nos[$i]){
               $this->inwords.=$this->vals[$i].” “.”Hundred”;
               if($this->rem!=0){
                   $this->inwords.=” “;
                   $this->n=$this->rem;
                   if(($this->rem>9)&&($this->rem                       for($j=0;$jnos);$j++){
                           if($this->rem==$this->nos[$j]){
                               $this->inwords.=$this->vals[$j];
                               break;
                           }
                       }
                   }
                   else if(($this->rem>19)&&($this->rem                       $this->call2($this->n);
                       $this->tens();
                   }
                   else{
                       $this->ones();
                   }
               }
               break;
            }
         
           }
      }
      function thousand(){
           for($i=0;$inos);$i++){
            if($this->cth==$this->nos[$i]){
               $this->inwords.=$this->vals[$i].” “.”Thousand”;
               if($this->rem!=0){
                   $this->inwords.=” “;
                   $this->n=$this->rem;
                   if(($this->rem>9)&&($this->rem                       for($j=0;$jnos);$j++){
                           if($this->rem==$this->nos[$j]){
                               $this->inwords.=$this->vals[$j];
                               break;
                           }
                       }
                   }
                   else if(($this->rem>19)&&($this->rem                       $this->call2($this->n);
                       $this->tens();
                   }
                   else if(($this->rem>99)&&($this->rem                       $this->call3($this->n);
                       $this->hundred();
                   }
                   else{
                       $this->ones();
                   }
               }
               break;
            }
         
           }
      }
      function tenThousand(){
          $flag=0;
          /* $flag will be set as 1 in the first for loop if ‘if() is true else will by default have 0’
          *  use: e.g. 10,000 i.e for value 10 which is present in array the corresponding
          *  word will be attached or concatenated in case it is 21 or the value which is not present in
          *  the array it will call the tens method to produce a valid two digit read from the left side of number
          */
          $r=$this->rem;
          /* in case number is 10000, 21000,22000,… there will be multiple
          *  methos called which will ask for rem so to preserve the value for other
          *  calls we are using $r for local checks
          */
           for($i=0;$inos);$i++){
            if($this->cth==$this->nos[$i]){
               $this->inwords.=$this->vals[$i].” “.”Thousand”;
               $flag=1;
               break;
            }
           }
           if($flag!=1){
               $this->n=$this->cth;
               $this->call2($this->n);
               $this->tens();
               $this->inwords.=” “.”Thousand”;
           }
           if($r!=0){
                   $this->inwords.=” “;
                   $this->n=$r;
                   if(($r>9)&&($r                       for($j=0;$jnos);$j++){
                           if($r==$this->nos[$j]){
                               $this->inwords.=$this->vals[$j];
                               break;
                           }
                       }
                   }
                   else if(($r>19)&&($r                       $this->call2($this->n);
                       $this->tens();
                   }
                   else if(($r>99)&&($r                       $this->call3($this->n);
                       $this->hundred();
                   }
                   else{
                       $this->ones();
                   }
               }
      }
      function lakhs(){
          $flag=0;
          /* $flag will be set as 1 in the first for loop if ‘if() is true else will by default have 0’
          *  use: e.g. 1,00,000 | 10,00,000 i.e for value 1 or 10 which is present in array the corresponding
          *  word will be attached or concatenated in case it is 21 or the value which is not present in
          *  the array it will call the tens method to produce a valid two digit read from the left side of number
          */
          $r=$this->rem;
          /* in case number is 10,00,000 | 20000… there will be multiple
          *  methos called which will ask for rem so to preserve the value for other
          *  calls we are using $r for local checks
          */
           for($i=0;$inos);$i++){
            if($this->clh==$this->nos[$i]){
               $this->inwords.=$this->vals[$i].” “.”Lakh”;
               $flag=1;
               break;
            }
           }
           if($flag!=1){
               $this->n=$this->clh;
               $this->call2($this->n);
               $this->tens();
               $this->inwords.=” “.”Lakh”;
           }
           if($r!=0){
                   $this->inwords.=” “;
                   $this->n=$r;
                   if(($r>9)&&($r                       for($j=0;$jnos);$j++){
                           if($r==$this->nos[$j]){
                               $this->inwords.=$this->vals[$j];
                               break;
                           }
                       }
                   }
                   else if(($r>19)&&($r                       $this->call2($this->n);
                       $this->tens();
                   }
                   else if(($r>99)&&($r                       $this->call3($this->n);
                       $this->hundred();
                   }
                   else if(($r>999)&&($r                       $this->call4($this->n);
                       $this->tenThousand();
                   }
                   else{
                       $this->ones();
                   }
               }
      }
      function crores(){
          $flag=0;
          /* $flag will be set as 1 in the first for loop if ‘if() is true else will by default have 0’
          *  use: e.g. 1,00,00,000 | 10,00,00,000 i.e for value 1 or 10 which is present in array the corresponding
          *  word will be attached or concatenated in case it is 21 or the value which is not present in
          *  the array it will call the tens method to produce a valid two digit read from the left side of number
          */
          $r=$this->rem;
          /* in case number is 10,00,00,000 | 20000… there will be multiple
          *  methos called which will ask for rem so to preserve the value for other
          *  calls we are using $r for local checks
          */
           for($i=0;$inos);$i++){
            if($this->cch==$this->nos[$i]){
               $this->inwords.=$this->vals[$i].” “.”Crore”;
               $flag=1;
               break;
            }
           }
           if($flag!=1){
               $this->n=$this->cch;
               $this->call2($this->n);
               $this->tens();
               $this->inwords.=” “.”Crore”;
           }
           if($r!=0){
                   $this->inwords.=” “;
                   $this->n=$r;
                   if(($r>9)&&($r                       for($j=0;$jnos);$j++){
                           if($r==$this->nos[$j]){
                               $this->inwords.=$this->vals[$j];
                               break;
                           }
                       }
                   }
                   else if(($r>19)&&($r                       $this->call2($this->n);
                       $this->tens();
                   }
                   else if(($r>99)&&($r                       $this->call3($this->n);
                       $this->hundred();
                   }
                   else if(($r>999)&&($r                       $this->call4($this->n);
                       $this->tenThousand();
                   }
                   else if(($r>99999)&&($r                       $this->call5($this->n);
                       $this->lakhs();
                   }
                   else{
                       $this->ones();
                   }
               }
      }
  }//end of class convert
?>

$value=$_GET["textfield”];
if(is_numeric($value)){
      if(ctype_digit ( $value )){
             //echo ‘Got Value :’.$value;
             $obj=new convertor($value);
      }else{
             echo ‘Got Value :’.$value;
             echo “
“;
             $value=round($value);
             echo ‘New Value after Round Up:’.$value;
             echo “
“;
             $obj=new convertor($value);
      }
$inwords=$obj->convert();
if($inwords)
echo “

“.$value.” : “.$inwords.”

Go Back“;
}else{
                echo “You have Entered an Invalid Number. Try Again!”;
}
?>


Now use a server to run the index.html and convert.php file. If this the first visit to PHP post please go through introductory posts to know about PHP, its installations and downloads and running your application using a web server.

Visit downloads section of website to download wamp server.

Click here to run the program.