Ari Wibowo

Software Engineer Enthusias | Read Write Code Traveling |

About This Site

This may be a good place to introduce yourself and your site or include some credits.

Find Us

Address
123 Main Street
New York, NY 10001

Hours
Monday–Friday: 9:00AM–5:00PM
Saturday & Sunday: 11:00AM–3:00PM

Codeigniter – Membuat Helper Untuk Fungsi Terbilang

Pada artikel kali ini, saya akan berbagi tentang Tutorial Codeigniter – Membuat Helper Untuk Fungsi Terbilang.

Misal 1.000.000 dikonversi menjadi terbilang : Satu Juta
Pertama kita buat file terbilang_helper.php di directory application/helper/ :

[php]
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’); /** * to terbilang * * Returns IDR number to string . This is a helper function * for various other ones in this library * * @access public * @param string value * @return string */ if ( ! function_exists(‘to_terbilang’)) { function to_terbilang($n) { $dasar = array(1 => ‘satu’, ‘dua’, ‘tiga’, ’empat’, ‘lima’, ‘enam’,’tujuh’, ‘delapan’, ‘sembilan’);
$angka = array(1000000000000, 1000000000, 1000000, 1000, 100, 10, 1);
$satuan = array(‘triliyun’, ‘milyar’, ‘juta’, ‘ribu’, ‘ratus’, ‘puluh’, ”);

$i = 0;
$str=”;
if($n==0){
$str = "";
}else{
while ($n != 0) {
$count = (int)($n/$angka[$i]);
if ($count >= 10) {
$str .= to_terbilang($count). " ".$satuan[$i]." ";
}else if($count > 0 && $count < 10){ $str .= $dasar[$count] . " ".$satuan[$i]." "; } $n -= $angka[$i] * $count; $i++; } $str = preg_replace("/satu puluh (\w+)/i", "\\1 belas", $str); $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i", "se\\1", $str); } return ucwords($str); } } ?>
[/php]

Cara penggunaannya seperti kita biasa menggunakan helper pada codeigniter, bisa memanggil helper pada controller, atau pada config/autoload.php.

Kita coba buat controller terbilang.php pada directory application/controllers :

[php]
&lt;?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’); class terbilang extends CI_Controller { public function index() { $this-&gt;load-&gt;helper("terbilang");
echo ucwords(number_to_words("87,5"));
}
}
[/php]