SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | Class Terbilang { | |
3 | ||
4 | public static $dasar = array(1=>'satu','dua','tiga','empat','lima','enam','tujuh','delapan','sembilan'); | |
5 | public static $angka = array(1000000000,1000000,1000,100,10,1); | |
6 | public static $satuan = array('milyar','juta','ribu','ratus','puluh',''); | |
7 | ||
8 | public static function eja($n) { | |
9 | $i=0; | |
10 | $str=""; | |
11 | while($n!=0){ | |
12 | $count = (int)($n/self::$angka[$i]); | |
13 | if($count>=10) $str .= self::eja($count). " ".self::$satuan[$i]." "; | |
14 | else if($count > 0 && $count < 10) | |
15 | $str .= self::$dasar[$count] . " ".self::$satuan[$i]." "; | |
16 | $n -= self::$angka[$i] * $count; | |
17 | $i++; | |
18 | } | |
19 | $str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str); | |
20 | $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str); | |
21 | return $str; | |
22 | } | |
23 | } | |
24 | foreach(range(1, 1000) as $i) | |
25 | - | echo Terbilang::eja(101); |
25 | + | echo Terbilang::eja($i).PHP_EOL; |