Zero-Affect
06-09-2010, 06:02 PM
function currency_format($amount, $currency='') {
$currency_array = array(
'GBP' => '£',
'JPY' => '¥',
'CNY' => '¥',
'EUR' => '€',
'USD' => '$',
'' => ''
);
if ( (ctype_alpha($currency) OR empty($currency)) && array_key_exists($currency, $currency_array) && ctype_digit($amount) ) {
$ret_txt = '<span style="font-weight: bold;">'.$currency_array[$currency].'</span>'.number_format($amount);
} else {
$ret_txt = FALSE;
}
return $ret_txt;
}
$var = '2001';
echo currency_format($var, 'USD'); // outputs $2,001
echo currency_format($var, 'GBP'); // outputs £2,001
echo currency_format($var, 'JPY'); // outputs ¥2,001
echo currency_format($var, 'CNY'); // outputs ¥2,001
echo currency_format($var, 'EUR'); // outputs €2,001
echo currency_format($var); // outputs 2,001
echo currency_format($var, 'lol'); // fails
echo currency_format('+'.$var, 'USD'); // fails
I was bored and needed something like this so i made it and thought i'd post my result
$currency_array = array(
'GBP' => '£',
'JPY' => '¥',
'CNY' => '¥',
'EUR' => '€',
'USD' => '$',
'' => ''
);
if ( (ctype_alpha($currency) OR empty($currency)) && array_key_exists($currency, $currency_array) && ctype_digit($amount) ) {
$ret_txt = '<span style="font-weight: bold;">'.$currency_array[$currency].'</span>'.number_format($amount);
} else {
$ret_txt = FALSE;
}
return $ret_txt;
}
$var = '2001';
echo currency_format($var, 'USD'); // outputs $2,001
echo currency_format($var, 'GBP'); // outputs £2,001
echo currency_format($var, 'JPY'); // outputs ¥2,001
echo currency_format($var, 'CNY'); // outputs ¥2,001
echo currency_format($var, 'EUR'); // outputs €2,001
echo currency_format($var); // outputs 2,001
echo currency_format($var, 'lol'); // fails
echo currency_format('+'.$var, 'USD'); // fails
I was bored and needed something like this so i made it and thought i'd post my result