<?php
class in_words{
    
  var $word = "";
  var $decimal;
  var $decimal_text = '';
  
  function in_words(){                       
	   $this->aa = array(" ", "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion");  
	   $this->tens = array(" ", " ", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty");
	   $this->on = array("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen");
	   $this->ones = array(" ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"); 
  }
  
  function show_words($fig){ 
	   $this->fig = ereg_replace(",", "", $fig);              // REMEOVE ANY , FROM THE NUMBER
	   $this->spilt_decimals();                               // CHECK FOR THE DECIMAL PART
	   
	   $this->rr = explode(",", number_format($this->fig));   // SEPARATE THE 3 DIGITS INTO ARRAY ELEMENTS	   
	    $this->mx = count($this->rr);
	   $this->fig = strtolower($this->fig);  
	   $this->compose();
	   $this->handle_decimal(); 
	   return $this->word.($this->decimal_text ? " <b>point</b> ".$this->decimal_text : '');
  }
  
  function spilt_decimals(){
	  $n = explode(".", $this->fig);
	  $this->fig = $n[0];
	  $this->decimal = $n[1];
  }
  
  function compose(){
	    $this->word = ''; // TO RESET THE VALUE FOR MULTIPLE INSTANCES
		if($this->mx == 1 && $this->rr[0] == 0) $this->word = "Zero"; 
		else if($this->mx > 6) $this->word = "Figure NOT available in words"; // OUT OF THE quadrillion range JUST IGNORE
		else{
			for($i=0; $i<$this->mx; $i++){
				$k = $this->mx - $i;
				$this->word .= $this->handle_3($this->rr[$i])." ".($k > 1 ? ($this->rr[$i] == '000' ? '' : $this->aa[$k]). " " : '');	   
			}
		}
  }
  
  function handle_3($num){
		
		$num = $num[2].$num[1].$num[0];
		if($num[1] == '1') $text = ($num[2] ? $this->ones[$num[2]]." Hundred ": ""). ($num[1] ? $this->on[$num[0]]." " : ""); 
		
		else $text = ($num[2] ? $this->ones[$num[2]]." Hundred ": ""). ($num[1] ? $this->tens[$num[1]]." " : "").$this->ones[$num[0]]; 
		
	  return $text;
  }
  
  function handle_decimal(){
	 $this->decimal_text = "";
     for($i=0; $i< strlen($this->decimal); $i++){
		 if($this->decimal[$i] == 0) $this->decimal_text .= " Zero ";
		 else $this->decimal_text .= " ".$this->ones[$this->decimal[$i]];
     }
	 
  }
  
}
  
?>