Location: PHPKode > scripts > dea num2word > numtoword.class.php
<?php
if(isset($_GET[deavalue]) && (isset($_GET[dealang]) || isset($_GET[deaformatlang]))){
   $value = $_GET[deavalue];
   $lang = $_GET[dealang];
   $formatlang = $_GET[deaformatlang];
   if(isset($_GET[deacurrency]))$currency = $_GET[deacurrency];
   else$currency = '';
   if(isset($_GET[deaseparator]))$separator = $_GET[deaseparator];
   else$separator = ' ';
   if(isset($_GET[deadebug]))$debug = $_GET[deadebug];
   else$debug = 0;

   $n2w = new DeaNum2Word($value,$lang,$currency,$separator,$debug,$formatlang);
   $out = $n2w->Num2WordOut();
   echo($out);
   echo '<br /><input type=button value=back onclick="history.go(-1)";>';
}

class DeaNum2Word{
   public function __construct($value,$lang,$currency = '',$separator = '',$debug = 0,$formatlang = ''){
      $dot = chr(46);
      $coma = chr(44);
      $this->debug = $debug;

      if($value == '')$value = '0';
      $incoma = strpos(strrev($value),$coma) ? strpos(strrev($value),$coma) : 99;
      $indot = strpos(strrev($value),$dot) ? strpos(strrev($value),$dot) : 99;;
      if($incoma < $indot){$decsym = $coma;$value = str_replace($dot,'',$value);}
      else{$decsym = $dot;$value = str_replace($coma,'',$value);}

      if($formatlang == '')$this->numword = $this->Num2WordLang($lang);
      else{
         for ($x = 0; $x < count($formatlang); $x = $x + 2){
            $fmttmp[$formatlang[$x+1]] = $formatlang[$x];
         }
         $this->numword = $fmttmp;
      }

      if(strval($value)<pow(1000,4)){
         if($this->debug <> 0){
            if(count($this->numword) > 0){
               echo '<table border="1" width="100%" cellspacing="0">';
               foreach($this->numword as $key => $word){
                  echo '<tr><td>'.$word.' - '.$key.'</tr></td>';
               }
               echo '</table>';
            }
         }
         if(count($this->numword) > 0){
            $tmp = explode($decsym,$value);
            $integer = $tmp[0];
            $decimal = substr($tmp[1].'00',0,2);

            //echo $this->Num2Word($integer,$separator).'<br>';
            $this->out = $this->Num2Word($integer,$separator).' ';

            $str = array_search('formatdec',$this->numword);
            eval($str);
            if($fmt == '' || $dec == ''){
               $fmt = ' %s/100';
               $dec = $decimal;
            }
            //echo $this->Num2Word($decimal,$separator).'<br>';
            $this->out .= sprintf($fmt,$dec).' '.$currency;
         }
      }else{
         $err = array_search('error',$this->numword);
         if($err == '') $err = 'Number out of range';
         $this->out = $err;
      }
   }

   // output
   public function Num2WordOut(){
      return $this->out;
   }

   // create num to word
   private function Num2Word($value,$separator = ''){
      $n0 = $value;
      $o = '';
      $i = 3;
      //999,999,999,999.99
      while($i >= 0){
         $m = pow(1000,$i);
         $n = floor($n0 / $m) * $m;
         $x = $this->Num2WordHandle($n / $m);
         // postavi u niz da bude zanimljivije !!!
         if($x[0]<>0)$n100 = array_search($x[0],$this->numword);else$n100 = '';
         if($x[1]<>0)$n10 = array_search($x[1],$this->numword);else$n10 = '';
         if($x[2]<>0)$n1 = array_search($x[2],$this->numword);else$n1 = '';

         if($i > 0 && ($x[0]<>0 || $x[1]<>0 || $x[2]<>0)){
            $n1000x = array_search($m,$this->numword);
         }else{$n1000x = '';}

         // format exception
         $str = array_search('exception',$this->numword);
         eval($str);

         // format output
         $str = array_search('formatout',$this->numword);
         $intout = '';
         eval($str);
         if($intout == ''){
            $intout = $n100.$separator.$n10.$separator.$n1.$separator.$n1000x.$separator;
         }

         if($this->debug <> 0){
            echo '<table border="1" width="100%" cellspacing="0">';
            if($n100 !== '')echo '<tr><td>'.$n100.'</td></tr>';
            if($n10 !== '')echo '<tr><td>'.$n10.'</td></tr>';
            if($n1 !== '')echo '<tr><td>'.$n1.'</td></tr>';
            if($n1000x !== '')echo '<tr ><td>'.$n1000x.'</td></tr>';
            if($n100 !== '' || $n10 !== '' || $n1 !== '' || $n1000x !== '')echo '<tr><td bgcolor="#DDDDDD"><b>'.$intout.'</b></td></tr>';
            //echo str_repeat('-',50).'<br>';
            echo '</table><br />';
         }

         $o .= $intout;
         $o = str_replace($separator.$separator,$separator,$o);

         $n0 = $n0 - $n;
         $i--;
      }
      $o = str_replace($separator.$separator,$separator,$o);
      $o = substr($o,0,strlen($o)-1);
      return $o;
   }

   function Num2WordHandle($int){
      $n100 = floor($int / 100);
      $int -= $n100 * 100;
      $n100 = $n100 * 100;
      if($int > 19){
         $n10 = floor($int / 10) * 10;
         $n1 = $int % 10;
      }else{
         if($int > 9){
            $n10 = $int;
            $n1 = 0;
         }else{
            $n10 = 0;
            $n1 = $int;
         }
      }
      return array($n100,$n10,$n1);
   }
   // format language
   public function Num2WordLang($lang){
      if($lang == 'srl')
         $numword = array(1,'jedan',2,'dva',3,'tri',4,'&#269etiri',5,'pet',
         6,'&#353est',7,'sedam',8,'osam',9,'devet',10,'deset',
         11,'jedanaest',12,'dvanaest',13,'trinaest',14,'&#269etrnaest',15,'petnaest',
      	16,'&#353esnaest',17,'sedamnaest',18,'osamnaest',19,'devetnaest',
      	20,'dvadeset',30,'trideset',40,'&#269etrdeset',50,'pedeset',
      	60,'&#353ezdeset',70,'sedamdeset',80,'osamdeset',90,'devedeset',
      	100,'sto',200,'dvesta',300,'trista',400,'&#269etirsto',500,'petsto',
         600,'&#353esto',700,'sedamsto',800,'osamsto',900,'devetsto',
         1000,'hiljada',1000000,'milion',1000000000,'milijarda',
         'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
            if($i == 1){$padez = \'a\';$n1 = str_replace(\'jedan\',\'jedna\',$n1);if($n1 == \'dva\' || $n1 == \'tri\' || $n1 == \'&#269etiri\')$padez = \'e\';if($n1 == \'dva\')$n1 = \'dve\';$n1000x = substr($n1000x,0,strlen($n1000x)-1).$padez;}
            if($i == 2){$padez = \'a\';if($n1 == \'jedan\')$padez = \'\';$n1000x .= $padez;}
            if($i == 3){$padez = \'i\';if($n1 == \'jedan\'){$n1 == \'jedna\';$padez = \'a\';}if($n1 == \'dva\' || $n1 == \'tri\' || $n1 == \'&#269etiri\')$padez = \'e\';$n1000x = substr($n1000x,0,strlen($n1000x)-1).$padez;}
         }');

      if($lang == 'src')
         $numword = array(1,'&#1112&#1077&#1076&#1072&#1085',2,'&#1076&#1074&#1072',3,'&#1090&#1088&#1080',4,'&#1095&#1077&#1090&#1080&#1088&#1080',5,'&#1087&#1077&#1090',
         6,'&#1096&#1077&#1089&#1090',7,'&#1089&#1077&#1076&#1072&#1084',8,'&#1086&#1089&#1072&#1084',9,'&#1076&#1077&#1074&#1077&#1090',10,'&#1076&#1077&#1089&#1077&#1090',
         11,'&#1081&#1077&#1076&#1072&#1085&#1072&#1077&#1089&#1090',12,'&#1076&#1074&#1072&#1085&#1072&#1077&#1089&#1090',13,'&#1090&#1088&#1080&#1085&#1072&#1077&#1089&#1090',14,'&#1095&#1077&#1090&#1088&#1085&#1072&#1077&#1089&#1090',15,'&#1087&#1077&#1090&#1085&#1072&#1077&#1089&#1090',
         16,'&#1096&#1077&#1089&#1090&#1085&#1072&#1077&#1089&#1090',17,'&#1089&#1077&#1076&#1072&#1084&#1085&#1072&#1077&#1089&#1090',18,'&#1086&#1089&#1072&#1084&#1085&#1072&#1077&#1089&#1090',19,'&#1076&#1077&#1074&#1077&#1090&#1085&#1072&#1077&#1089&#1090',
         20,'&#1076&#1074&#1072&#1076&#1077&#1089&#1077&#1090',30,'&#1090&#1088&#1080&#1076&#1077&#1089&#1077&#1090',40,'&#1095&#1077&#1090&#1088&#1076&#1077&#1089&#1077&#1090',50,'&#1087&#1077&#1076&#1077&#1089&#1077&#1090',
         60,'&#1096&#1077&#1079&#1076&#1077&#1089&#1077&#1090',70,'&#1089&#1077&#1076&#1072&#1084&#1076&#1077&#1089&#1077&#1090',80,'&#1086&#1089&#1072&#1084&#1076&#1077&#1089&#1077&#1090',90,'&#1076&#1077&#1074&#1077&#1090&#1076&#1077&#1089&#1077&#1090',
         100,'&#1089&#1090&#1086',200,'&#1076&#1074&#1077&#1089&#1090&#1072',300,'&#1090&#1088&#1080&#1089&#1090&#1072',400,'&#1095&#1077&#1090&#1080&#1088&#1089&#1090&#1086',
         500,'&#1087&#1077&#1090&#1089&#1090&#1086',600,'&#1096&#1077&#1089&#1090&#1086',700,'&#1089&#1077&#1076&#1072&#1084&#1089&#1090&#1086',800,'&#1086&#1089&#1072&#1084&#1089&#1090&#1086',900,'&#1076&#1077&#1074&#1077&#1090&#1089&#1090&#1086',
         1000,'&#1093&#1080&#1113&#1072&#1076',1000000,'&#1084&#1080&#1083&#1080&#1086&#1085',1000000000,'&#1084&#1080&#1083&#1080&#1081&#1072&#1088&#1076',
         'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
            if($i == 1){$padez = \'&#1072\';$n1 = str_replace(\'&#1112&#1077&#1076&#1072&#1085\',\'&#1112&#1077&#1076&#1085&#1072jedna\',$n1);if($n1 == \'&#1076&#1074&#1072\' || $n1 == \'&#1090&#1088&#1080\' || $n1 == \'&#1095&#1077&#1090&#1080&#1088&#1080\')$padez = \'e\';if($n1 == \'&#1076&#1074&#1072\')$n1 = \'&#1076&#1074&#1077\';$n1000x .= $padez;}
            if($i == 2){$padez = \'&#1072\';if($n1 == \'&#1112&#1077&#1076&#1072&#1085\')$padez = \'\';$n1000x .= $padez;}
            if($i == 3){$padez = \'&#1080\';if($n1 == \'&#1112&#1077&#1076&#1072&#1085\'){$n1 == \'&#1112&#1077&#1076&#1085&#1072\';$padez = \'&#1072\';}if($n1 == \'&#1076&#1074&#1072\' || $n1 == \'&#1090&#1088&#1080\' || $n1 == \'&#1095&#1077&#1090&#1080&#1088&#1080\')$padez = \'&#1077\';$n1000x .= $padez;}
         }');

      if($lang == 'hr')
         $numword = array(1,'jedan',2,'dva',3,'tri',4,'&#269etiri',5,'pet',
         6,'&#353est',7,'sedam',8,'osam',9,'devet',10,'deset',
         11,'jedanaest',12,'dvanaest',13,'trinaest',14,'&#269etrnaest',15,'petnaest',
      	16,'&#353esnaest',17,'sedamnaest',18,'osamnaest',19,'devetnaest',
      	20,'dvadeset',30,'trideset',40,'&#269etrdeset',50,'pedeset',
      	60,'&#353ezdeset',70,'sedamdeset',80,'osamdeset',90,'devedeset',
      	100,'sto',200,'dvesta',300,'trista',400,'&#269etirsto',500,'petsto',
         600,'&#353esto',700,'sedamsto',800,'osamsto',900,'devetsto',
         1000,'tisu&#263a',1000000,'miliun',1000000000,'milijarda',
         'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
            if($i == 1){$padez = \'a\';$n1 = str_replace(\'jedan\',\'jedna\',$n1);if($n1 == \'dva\' || $n1 == \'tri\' || $n1 == \'&#269etiri\')$padez = \'e\';if($n1 == \'dva\')$n1 = \'dve\';$n1000x = substr($n1000x,0,strlen($n1000x)-1).$padez;}
            if($i == 2){$padez = \'a\';if($n1 == \'jedan\')$padez = \'\';$n1000x .= $padez;}
            if($i == 3){$padez = \'i\';if($n1 == \'jedan\'){$n1 == \'jedna\';$padez = \'a\';}if($n1 == \'dva\' || $n1 == \'tri\' || $n1 == \'&#269etiri\')$padez = \'e\';$n1000x = substr($n1000x,0,strlen($n1000x)-1).$padez;}
         }');

      if($lang == 'en')
         $numword = array(
         1,'one',2,'two',3,'three',4,'four',5,'five',
         6,'six',7,'seven',8,'eight',9,'nine',10,'ten',
         11,'eleven',12,'twelve',13,'thirteen',14,'fourteen',15,'fifteen',
      	16,'sixteen',17,'seventeen',18,'eighteen',19,'nineteen',
         20,'twenty',30,'thirty',40,'fourty',50,'fifty',
      	60,'sixty',70,'seventy',80,'eighty',90,'ninety',
         100,'one hundred',200,'two hundred',300,'three hundred',400,'four hundred',500,'five hundred',
         600,'six hundred',700,'seven hundred',800,'eight hundred',900,'nine hundred',
         1000,'thousand',1000000,'million',1000000000,'billion',
         'formatdec','if($decimal<>0)$fmt = \'point%s\';else $fmt = \'%s\';$dec = $this->Num2Word($decimal,$separator);');

      if($lang == 'de')
         $numword = array(
         1,'ein',2,'zwei',3,'drei',4,'vier',5,'f&#252nf',
         6,'sechs',7,'sieben',8,'acht',9,'neun',10,'zehn',
         11,'elf',12,'zw&#246lf',13,'dreizig ',14,'vierzig ',15,'f&#252nfzehn',
         16,'sechszig',17,'siebenzig',18,'achtzig',19,'neunzig',
      	20,'zwanzig',30,'dreizig',40,'vierzig',50,'f&#252nfzig',
         60,'sechszig',70,'siebenzig',80,'achtzig',90,'neunzig',
         100,'ein hundert',200,'zwei hundert',300,'drei hundert',400,'vier hundert',500,'f&#252nf hundert',
         600,'sechs hundert',700,'sieben hundert',800,'acht hundert',900,'neun hundert',
         1000,'tausend',1000000,'millon',1000000000,'billon',
         'exception','if($n1 !== \'\' && $n10 !== \'\')$n1 .= \' und \';',
         'formatout','$intout = $n100.$separator.$n1.$separator.$n10.$separator.$n1000x.$separator;',
         'formatdec','if($decimal<>0)$fmt = \'mit%s\';else $fmt = \'%s\';$dec = $this->Num2Word($decimal,$separator);');

      if($lang == 'ru')
         $numword = array(1,'&#1086&#1076&#1080&#1085',2,'&#1076&#1074&#1072',3,'&#1090&#1088&#1080',4,'&#1095&#1077&#1090&#1099&#1088&#1077',5,'&#1087&#1103&#1090&#1100',
         6,'&#1096&#1077&#1089&#1090&#1100',7,'&#1089&#1077&#1084&#1100',8,'&#1074&#1086&#1089&#1077&#1084&#1100',9,'&#1076&#1077&#1074&#1103&#1090&#1100',10,'&#1076&#1077&#1089&#1103&#1090&#1100',
         11,'&#1086&#1076&#1080&#1085&#1085&#1072&#1076&#1094&#1072&#1090&#1100',12,'&#1076&#1074&#1077&#1085&#1072&#1076&#1094&#1072&#1090&#1100',13,'&#1090&#1088&#1080&#1085&#1072&#1076&#1094&#1072&#1090&#1100',14,'&#1095&#1077&#1090&#1099&#1088&#1085&#1072&#1076&#1094&#1072&#1090&#1100',15,'&#1087&#1103&#1090&#1085&#1072&#1076&#1094&#1072&#1090&#1100',
         16,'&#1096&#1077&#1089&#1090&#1085&#1072&#1076&#1094&#1072&#1090&#1100',17,'&#1089&#1077&#1084&#1085&#1072&#1076&#1094&#1072&#1090&#1100',18,'&#1074&#1086&#1089&#1077&#1084&#1085&#1072&#1076&#1094&#1072&#1090&#1100',19,'&#1076&#1077&#1074&#1103&#1090&#1085&#1072&#1076&#1094&#1072&#1090&#1100',
         20,'&#1076&#1074&#1072&#1076&#1094&#1072&#1090&#1100',30,'&#1090&#1088&#1080&#1076&#1094&#1072&#1090&#1100',40,'&#1089&#1086&#1088&#1086&#1082',50,'&#1087&#1103&#1090&#1100&#1076&#1077&#1089&#1103&#1090',
         60,'&#1096&#1077&#1089&#1090&#1100&#1076&#1077&#1089&#1103&#1090',70,'&#1089&#1077&#1084&#1100&#1076&#1077&#1089&#1103&#1090',80,'&#1074&#1086&#1089&#1077&#1084&#1100&#1076&#1077&#1089&#1103&#1090',90,'&#1076&#1077&#1074&#1103&#1085&#1086&#1089&#1090&#1086',
         100,'&#1089&#1090&#1086',200,'&#1076&#1074&#1077&#1089&#1090&#1080',300,'&#1090&#1088&#1080&#1089&#1090&#1072',400,'&#1095&#1077&#1090&#1099&#1088&#1077&#1089&#1090&#1072',
         500,'&#1087&#1103&#1090&#1100&#1089&#1086&#1090',600,'&#1096&#1077&#1089&#1090&#1100&#1089&#1086&#1090',700,'&#1089&#1077&#1084&#1100&#1089&#1086&#1090',800,'&#1074&#1086&#1089&#1077&#1084&#1100&#1089&#1086&#1090',900,'&#1076&#1077&#1074&#1103&#1090&#1100&#1089&#1086&#1090',
         1000,'&#1090&#1099&#1089&#1103&#1095',1000000,'&#1084&#1080&#1083&#1083&#1080&#1086&#1085',1000000000,'&#1073&#1080&#1083&#1083&#1080&#1086&#1085',
         'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
         if($i == 1){$padez = \'&#1072\';if($n1 == \'&#1076&#1074&#1072\' || $n1 == \'&#1090&#1088&#1080\' || $n1 == \'&#1095&#1077&#1090&#1099&#1088&#1077\')$padez = \'&#1077\';if($n1 == \'&#1076&#1074&#1072\')$n1 = \'&#1076&#1074&#1077\';$n1000x .= $padez;}
         if($i == 2  || $i == 3 ){$padez = \'&#1072\';if($n1 == \'&#1086&#1076&#1080&#1085\')$padez = \'\';$n1000x .= $padez;}}');

      if($lang == 'it')
         $numword = array(
         1,'uno',2,'due',3,'tré',4,'quattro',5,'cinque',
         6,'seiv',7,'sette',8,'otto',9,'nove',10,'dieci',
         11,'undici',12,'dodici',13,'tredici',14,'quattordici',15,'quindici',
         16,'sedici',17,'diciassette',18,'diciotto',19,'diciannove',
         20,'venti',21,'ventuno',28,'ventotto',30,'trenta',40,'quaranta',50,'cinquanta',
         60,'sessanta',70,'settanta',80,'ottanta',90,'novanta',
         100,'cento',200,'duecento',300,'trecento',400,'quattrocento',500,'cinquecento',
         600,'seicento',700,'settecento',800,'ottocento',900,'novecento',
         1000,'mille',1000000,'milione',1000000000,'bilione',
         'exception','if($n10 == \'venti\' && ($n1 == \'uno\' || $n1 == \'otto\'))$n10 = \'vent\';');

      if($lang == 'pt')
         $numword = array(
         1,'um',2,'dois',3,'três',4,'quatro',5,'cinco',
         6,'seis',7,'sete',8,'oito',9,'nove',10,'dez',
         11,'onze',12,'doze',13,'treze',14,'quatorze',15,'quinze',
         16,'dezesseis',17,'dezessete',18,'dezoito',19,'dezenove',
         20,'vinte',21,'vinte e um',21,'vinte e um',22,'vinte dois',23,'vinte três',24,'vinte quatro',
         30,'trinta',31,'trinta uns',40,'quarenta',50,'cinqüênta',
         60,'sessenta',70,'setenta',80,'oitenta',90,'noventa',
         100,'cem',200,'duzentos',300,'trezentos',400,'quatrocentos',500,'quinhentos',
         600,'seiscentos',700,'setecentos',800,'oitocentos',900,'novecentos',
         1000,'mil',1000000,'milhão',1000000000,'bilhão',
         'exception','if($n10 !== \'\')$n10 .= \' e \';');

      if($lang == 'sp')
         $numword = array(
         1,'uno',2,'dós',3,'trés',4,'cuatro',5,'cinco',
         6,'séis',7,'siete',8,'ocho',9,'nueve',10,'diez',
         11,'once',12,'doce',13,'trece',14,'catorce',15,'quince',
         16,'dieciséis',17,'diecisiete',18,'dieciocho',19,'diecinueve',
         20,'veinte',30,'treinta',40,'cuarenta',50,'cincuenta',
         60,'sesenta',70,'setenta',80,'ochenta',90,'noventa',
         100,'ciento',200,'doscientos',300,'trescientos',400,'cuatrocientos',500,'quinientos',
         600,'seiscientos',700,'setecientos',800,'ochocientos',900,'novecientos',
         1000,'mil',1000000,'millón',1000000000,'mil millones',
         'exception','if($n10 !== \'\' && $n10 !== \'20\' && $n1 !== \'\')$n10 .= \' y \';');

      for ($x = 0; $x < count($numword); $x = $x + 2){
         $tmp[$numword[$x+1]] = $numword[$x];
      }
      return $tmp;
   }
}
?>

Return current item: dea num2word