<?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,'četiri',5,'pet',
6,'šest',7,'sedam',8,'osam',9,'devet',10,'deset',
11,'jedanaest',12,'dvanaest',13,'trinaest',14,'četrnaest',15,'petnaest',
16,'šesnaest',17,'sedamnaest',18,'osamnaest',19,'devetnaest',
20,'dvadeset',30,'trideset',40,'četrdeset',50,'pedeset',
60,'šezdeset',70,'sedamdeset',80,'osamdeset',90,'devedeset',
100,'sto',200,'dvesta',300,'trista',400,'četirsto',500,'petsto',
600,'šesto',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 == \'četiri\')$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 == \'četiri\')$padez = \'e\';$n1000x = substr($n1000x,0,strlen($n1000x)-1).$padez;}
}');
if($lang == 'src')
$numword = 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,'сто',200,'двеста',300,'триста',400,'четирсто',
500,'петсто',600,'шесто',700,'седамсто',800,'осамсто',900,'деветсто',
1000,'хиљад',1000000,'милион',1000000000,'милийард',
'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
if($i == 1){$padez = \'а\';$n1 = str_replace(\'један\',\'једнаjedna\',$n1);if($n1 == \'два\' || $n1 == \'три\' || $n1 == \'четири\')$padez = \'e\';if($n1 == \'два\')$n1 = \'две\';$n1000x .= $padez;}
if($i == 2){$padez = \'а\';if($n1 == \'један\')$padez = \'\';$n1000x .= $padez;}
if($i == 3){$padez = \'и\';if($n1 == \'један\'){$n1 == \'једна\';$padez = \'а\';}if($n1 == \'два\' || $n1 == \'три\' || $n1 == \'четири\')$padez = \'е\';$n1000x .= $padez;}
}');
if($lang == 'hr')
$numword = array(1,'jedan',2,'dva',3,'tri',4,'četiri',5,'pet',
6,'šest',7,'sedam',8,'osam',9,'devet',10,'deset',
11,'jedanaest',12,'dvanaest',13,'trinaest',14,'četrnaest',15,'petnaest',
16,'šesnaest',17,'sedamnaest',18,'osamnaest',19,'devetnaest',
20,'dvadeset',30,'trideset',40,'četrdeset',50,'pedeset',
60,'šezdeset',70,'sedamdeset',80,'osamdeset',90,'devedeset',
100,'sto',200,'dvesta',300,'trista',400,'četirsto',500,'petsto',
600,'šesto',700,'sedamsto',800,'osamsto',900,'devetsto',
1000,'tisuća',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 == \'četiri\')$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 == \'četiri\')$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ünf',
6,'sechs',7,'sieben',8,'acht',9,'neun',10,'zehn',
11,'elf',12,'zwölf',13,'dreizig ',14,'vierzig ',15,'fünfzehn',
16,'sechszig',17,'siebenzig',18,'achtzig',19,'neunzig',
20,'zwanzig',30,'dreizig',40,'vierzig',50,'fünfzig',
60,'sechszig',70,'siebenzig',80,'achtzig',90,'neunzig',
100,'ein hundert',200,'zwei hundert',300,'drei hundert',400,'vier hundert',500,'fünf 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,'один',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,'сто',200,'двести',300,'триста',400,'четыреста',
500,'пятьсот',600,'шестьсот',700,'семьсот',800,'восемьсот',900,'девятьсот',
1000,'тысяч',1000000,'миллион',1000000000,'биллион',
'exception','if($n1000x !== \'\' && ($n1 !== \'\' || $n10 !== \'\' || $n100 !== \'\')){
if($i == 1){$padez = \'а\';if($n1 == \'два\' || $n1 == \'три\' || $n1 == \'четыре\')$padez = \'е\';if($n1 == \'два\')$n1 = \'две\';$n1000x .= $padez;}
if($i == 2 || $i == 3 ){$padez = \'а\';if($n1 == \'один\')$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;
}
}
?>