Location: PHPKode > scripts > tkconvert > tkconvert/tkconvert.php
<?php 
/**
 * tkconvert.php :: Main tkconvert class and setup file
 *
 * tkconvert version 0.1.1
 * copyright (c) 2007 by Mahfuz -ur Rahman
 * 
 *
 * tkconvert is an open source PHP class library for easily convert money in words. 
 *
 * xajax is released under the terms of the LGPL license
 * http://www.gnu.org/copyleft/lesser.html#SEC3
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * FOR ANY SAGGETION PLESE CONTACT hide@address.com
 * 
 */

/*
   ----------------------------------------------------------------------------
   | example of this  class is given in tkconvert_example.php:   			
  | for bangla font go to http://www.ekushey.org/?page/rupali_layout													
   ----------------------------------------------------------------------------
*/

class tkconvert
{
	var $words = array();
	var $places = array();
	var $amounta = array();
	
	var $amount_in_words;
	var $amount;
	var $num;
	var $numlength;
	var $decimal;
	var $decimal_len;
	
	function tkconvert($am)
	{
		$this->amount = $am;
	}
	
	function setamount($m)
	{
		$this->amount = $m;
	}
	
	
	function convert($lan)
	{
		$this->assign($lan); //use language
	
		$temp = $this->amount;
	
 		$temp = (string)$this->amount;
		
		$pos = strpos($temp,".");
		$str = explode('.',$temp);
	
		$this->decimal = $str[1];
		$this->num = $str[0];
		
		if ( !empty($dec) ) 
		{
			$this->decimal = $dec;
			$this->decimal_len = strlen($this->decimal);
			$this->decimal = substr($this->decimal,1,$this->decimal_len+1);
		}
		
	/*	get last 2 digit	 */		
	
		$this->getlast2digit();
		
		$l1 = strlen($this->num);
		
			if ( $l1 != 0)
			{
			 	$this->gethundred();
			 }
		
		$l1 = strlen($this->num);
		
				
		if ( $l1 != 0)
			{
			 $this->getthousand();
			}
		
		
		$llac = strlen($this->num);
		
		
		if ( $llac != 0)
			{
			 	$this->getlac();
			 }
		
		$lcrore = strlen($this->num);
		
		if ( $lcrore != 0)
			{
				 $this->getcrore();
			}
	$lcrore2 = strlen($this->num);
		
		if ( $lcrore2 != 0)
			{
				$this->gethundred();
			}
	
	$lcrore3 = strlen($this->num);
		
		if ( $lcrore3 != 0)
			{
				$this->getthousand();
			}
	
	
	}
	
	
		
	function getlast2digit()
	{
		/*Last 2 Digit*/		
		
		$last2d = substr($this->num,-1,1);
		$lastzero = substr($this->num,-2,1);
			
		$last2f = substr($this->num,-2,2);
		
		if (array_key_exists($last2f, $this->words)) 
			{	
    			$s = $this->words[$last2f];	
			}
			
		else
		{
			$last2de = substr($this->num,-2,2);

			$last2di = $last2de - $last2d;
			
			$s = $this->words[$last2di]."".$this->words[$last2d];

		}	
			
		$l = $s;
		
		
		$t = $this->num;
		$numlen = strlen($this->num)-2;
		$this->num = substr($t,0,$numlen);

		//$l = $this->getlast($this->num);		
		
		if ( strcmp($last2f,'00') != 0 )
		{
		$fullword = $l.$this->places[0];
		
		array_push($this->amounta,$fullword);
		
		}
		
	}
	
	function gethundred()
	{
	// for hundred		
		
		
		$t  = substr($this->num,-1,1);
		if ( strcmp($t,'0') != 0 )
		{
			$l = $this->words[$t];
			$numlen = strlen($this->num)-1;
			$this->num = substr($this->num,0,$numlen);
			$fullword = $l.$this->places[1];
			array_push($this->amounta,$fullword);
		}
		else
		{
	
			$numlen = strlen($this->num)-1;
			$this->num = substr($this->num,0,$numlen);
		}
	
		

	}
	function getthousand()
	{
	
		$last2d = substr($this->num,-1,1);
		$lastzero = substr($this->num,-2,1);
		
		$last2f = substr($this->num,-2,2);
		
		if (array_key_exists($last2f, $this->words)) 
			{	
    			$s = $this->words[$last2f];	
			}
			
		else
		{
			$last2de = substr($this->num,-2,2);
			$last2di = $last2de - $last2d;
	
			$s = $this->words[$last2di]."".$this->words[$last2d];

		}	
			
		$l = $s;
		
		
		$t = $this->num;
		$numlen = strlen($this->num)-2;
		$this->num = substr($t,0,$numlen);
		
		if ( strcmp($last2f,'00') != 0 )
		{
		$fullword = $l.$this->places[2];
		
		array_push($this->amounta,$fullword);
		}
		
	}
	
	function getlac()
	{
		
		$last2d = substr($this->num,-1,1);
		$lastzero = substr($this->num,-2,1);
			
		$last2f = substr($this->num,-2,2);
		
		 
			if (array_key_exists($last2f, $this->words)) 
				{	
    				$s = $this->words[$last2f];	
				}
			
			else
			{
				$last2de = substr($this->num,-2,2);

				$last2di = $last2de - $last2d;
			
				$s = $this->words[$last2di]."".$this->words[$last2d];

		}
			$l = $s;
					
		
		$t = $this->num;
		$numlen = strlen($this->num)-2;
		$this->num = substr($t,0,$numlen);
			
		if ( strcmp($last2f,'00') != 0 )
		{
			
			$fullword = $l.$this->places[3];
		
			array_push($this->amounta,$fullword);
			
		}
	}

function getcrore()
	{
		
		$last2d = substr($this->num,-1,1);
		$lastzero = substr($this->num,-2,1);
		
		$last2f = substr($this->num,-2,2);
		
		if (array_key_exists($last2f, $this->words)) 
			{
    			$s = $this->words[$last2f];	
			}
			
		else
		{
			$last2de = substr($this->num,-2,2);

			$last2di = $last2de - $last2d;
			
			$s = $this->words[$last2di]."".$this->words[$last2d];
		}
			
		$l = $s;
		
		
		$t = $this->num;
		
		$numlen = strlen($this->num)-2;
		$this->num = substr($t,0,$numlen);
		$fullword = $l.$this->places[4 ];
		
		array_push($this->amounta,$fullword);
		
	}
	
	function display()
	{
		foreach ($this->amounta as $a)
		{
		 $this->amount_in_words = $this->amount_in_words.array_pop($this->amounta);
		//echo "<br>".$a;
		}
		return $this->amount_in_words;
	}
	function getNum()
	{
		return $this->amount;
	}
	function assign($lan = 'en') {
		
		switch ($lan)
		{
		case 'en':{
		$this->words["1"] = " One"; 		$this->words["2"] = " Two";
		$this->words["3"] = " Three"; 		$this->words["4"] = " Four";
		$this->words["5"] = " Five"; 		$this->words["6"] = " Six";
		$this->words["7"] = " Seven";		$this->words["8"] = " Eight";
		$this->words["9"] = " Nine";
	
		$this->words["10"] = " Ten";		$this->words["11"] = " Eleven";
		$this->words["12"] = " Twelve";		$this->words["13"] = " Thirten";
		$this->words["14"] = " Fourten";	$this->words["15"] = " Fiften";
		$this->words["16"] = " Sixten";		$this->words["17"] = " Seventen";
		$this->words["18"] = " Eighten";	$this->words["19"] = " Nineten";

		$this->words["20"] = " Twenty";		$this->words["30"] = " Thirty";
		$this->words["40"] = " Forty";		$this->words["50"] = " Fifty";
		$this->words["60"] = " Sixty";		$this->words["70"] = " Seventy";
		$this->words["80"] = " Eighty";		$this->words["90"] = " Ninety";
	
		
		$this->places[0] = " BDT";	
		$this->places[1] = " Hundred";				
		$this->places[2] = " Thousand";
		$this->places[3] = " Lac";		
		$this->places[4] = " Crore";}
		break;
		
		case 'bn':
		{
		$this->words["1"] = " এক"; 		$this->words["2"] = " দুই";
		$this->words["3"] = " তিন"; 		$this->words["4"] = " চার";
		$this->words["5"] = " পাচ"; 		$this->words["6"] = " ছয়";
		$this->words["7"] = " সাত";		$this->words["8"] = " আট";
		$this->words["9"] = " নয়";
		
		$this->words["10"] = " দশ";		$this->words["11"] = " এগারো";
		$this->words["12"] = " বারো";		$this->words["13"] = " তেরো";
		$this->words["14"] = " চোদ্দ";	      $this->words["15"] = " পনের";
		$this->words["16"] = " ষোল";		$this->words["17"] = " সতের";
		$this->words["18"] = " আঠার";  	$this->words["19"] = " উনিশ";

		$this->words["21"] = " একুশ";		$this->words["21"] = " বাইশ";
		$this->words["22"] = " বারো";		$this->words["23"] = " তেইশ";
		$this->words["24"] = " চব্বিশ";	      $this->words["25"] = " পচিশ";
		$this->words["26"] = " ছাব্বিশ";		$this->words["27"] = " সাতাশ";
		$this->words["28"] = " আঠাশ";	      $this->words["29"] = " উনত্রিশ";

		$this->words["41"] = " একচল্লিশ";	$this->words["42"] = " বিয়াল্লিশ";	
		$this->words["43"] = " তেতাল্লিশ";	$this->words["44"] = " চুয়াল্লিশ";		
		$this->words["45"] = " পয়তাল্লিশ";   	$this->words["46"] = " ছেচল্লিশ";		
		$this->words["47"] = " সাতচল্লিশ";    $this->words["48"] = " আটচল্লিশ";     
		$this->words["49"] = " উনপঞ্চাশ";    $this->words["51"] = " একান্ন";		
		$this->words["52"] = " বাহান্ন";	$this->words["53"] = " তিপান্ন ";
		$this->words["54"] = " চুয়ান্ন";		$this->words["55"] = " পঞ্চান্ন";
		$this->words["56"] = " ছাপানন";		$this->words["57"] = " সাতান্ন";
		$this->words["58"] = " আটান্ন";     $this->words["59"] = " উনষাট";

		$this->words["61"] = " একষট্টি";		$this->words["62"] = " বাষট্টি";	
		$this->words["63"] = " তেষট্টি"; 	$this->words["64"] = " চৌষট্টি";	
		$this->words["65"] = " পয়ষট্টি";		$this->words["66"] = " ছেষট্টি";		
		$this->words["67"] = " সাতষট্টি";    	$this->words["68"] = " আটষট্টি";      
		$this->words["69"] = " উনসত্তর";	$this->words["71"] = " একাত্তর";		
		$this->words["72"] = " বাহাত্তর";		$this->words["73"] = " তিহাত্তর ";
		$this->words["74"] = " চুয়াত্তর";		$this->words["75"] = " পচাত্তর";
		$this->words["76"] = " ছিয়াত্তর";		$this->words["77"] = " সাতাত্তর";
		$this->words["78"] = " আটাত্তর";    $this->words["79"] = " উনআশি";


		$this->words["81"] = " একাআশি";	$this->words["82"] = " বিরাআশি";	
		$this->words["83"] = " তিরাআশ"; 	$this->words["84"] = " চুরাআশি";	
		$this->words["85"] = " পচাআশি";	$this->words["86"] = " ছিয়াআশি";	
		$this->words["87"] = " সাতাআশি";  	$this->words["88"] = " আটাআশি";     
		$this->words["89"] = " উননব্বই";    $this->words["91"] = " একানব্বই";	
		$this->words["92"] = " বিরানব্বই";	$this->words["93"] = " তিরানব্বই";
		$this->words["94"] = " চুরানব্বই";	$this->words["95"] = " পচানব্বই";
		$this->words["96"] = " ছিয়ানব্বই";	$this->words["97"] = " সাতানব্বই";
		$this->words["98"] = " আটানব্বই";    $this->words["99"] = " নিরানব্বই";


		$this->words["20"] = " বিশ";		$this->words["30"] = " ত্রিশ";
		$this->words["40"] = " চল্লিশ";		$this->words["50"] = " পঞ্চাশ";
		$this->words["60"] = " ষাট";		$this->words["70"] = " সত্তর";
		$this->words["80"] = " আশি";		$this->words["90"] = " নব্বই";
		$this->words["100"] = " বিশ";		$this->words["200"] = " ত্রিশ";
		$this->words["300"] = " চল্লিশ";		$this->words["400"] = " পঞ্চাশ";
		$this->words["500"] = " ষাট";		$this->words["600"] = " সত্তর";
		$this->words["700"] = " আশি";		$this->words["800"] = " বিশ";
		$this->words["900"] = " আশি";

		
		$this->places[0] = " টাকা";	
		$this->places[1] = " শত";				
		$this->places[2] = " হাজার";
		$this->places[3] = " লাখ";		
		$this->places[4] = " কোটি";}
		break;
		}


	}
}
?>
Return current item: tkconvert