Last active
October 26, 2016 10:24
-
-
Save vanderson139/bdf7f93e51bda6ee9337cad1351afa2e to your computer and use it in GitHub Desktop.
Monefy - PHP Ultimate Money / Currency / Float Formatter and Fixer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Monefy - PHP Ultimate Money / Currency / Float Formatter and Fixer | |
|-------------------------------------------------------------------------- | |
| | |
| This function formats almost any input to currency and can also | |
| parse the formatted value as a float for database/backend operations | |
| | |
*/ | |
$test = [ | |
304, | |
540.834, | |
30010.00, | |
'30020,000, ', | |
'30050,00', | |
'30.050,00', | |
'30,050.03', | |
'100 130,050.03', | |
'30 050.023', | |
'30 050,023', | |
'100, 130,050.023', | |
'-100, 130,050.023', | |
'4485 4497 7224.8410', | |
'R$ 1,200.53', | |
'US$ 1.400,679', | |
'André Silva', | |
false, | |
null | |
]; | |
foreach ($test as $n) { | |
var_dump(monefy($n, 2, '.', ',')); // formated string for users/view | |
var_dump((float)monefy($n, 2, '.', '', '')); // parse float for database/backend | |
} | |
// outputs | |
// string(10) "US$ 304.00" | |
// float(304) | |
// string(10) "US$ 540.83" | |
// float(540.83) | |
// string(13) "US$ 30,010.00" | |
// float(30010) | |
// string(13) "US$ 30,020.00" | |
// float(30020) | |
// string(13) "US$ 30,050.00" | |
// float(30050) | |
// string(13) "US$ 30,050.00" | |
// float(30050) | |
// string(13) "US$ 30,050.03" | |
// float(30050.03) | |
// string(18) "US$ 100,130,050.03" | |
// float(100130050.03) | |
// string(13) "US$ 30,050.02" | |
// float(30050.02) | |
// string(13) "US$ 30,050.02" | |
// float(30050.02) | |
// string(18) "US$ 100,130,050.02" | |
// float(100130050.02) | |
// string(19) "US$ -100,130,050.02" | |
// float(-100130050.02) | |
// string(22) "US$ 448,544,977,224.84" | |
// float(448544977224.84) | |
// string(12) "US$ 1,200.53" | |
// float(1200.53) | |
// string(12) "US$ 1,400.68" | |
// float(1400.68) | |
// string(16) "US$ André Silva" | |
// float(0) | |
// string(4) "US$ " | |
// float(0) | |
// string(4) "US$ " | |
// float(0) | |
function monefy($input, $decimals = 2, $dec_point = '.', $thousands_sep = ',', $prefix = 'US$ ') { | |
// remove HTML encoded characters: http://stackoverflow.com/a/657670 | |
// special characters that arrive like &0234; | |
$input = preg_replace("/&#?[a-z0-9]{2,8};/i",'',$input); | |
// trim | |
$number = preg_replace('/^([^0-9\-]+)|([^0-9\-]+)$/', '', $input); | |
if(empty($number)) { | |
return $prefix . $input; | |
} | |
preg_match_all('/[^0-9\-]/', $number, $last_dec_point, PREG_OFFSET_CAPTURE); | |
while (is_array($last_dec_point)) { | |
$last_dec_point = end($last_dec_point); | |
} | |
if(empty($last_dec_point)) { | |
return $prefix . number_format($number , $decimals, $dec_point, $thousands_sep); | |
} | |
$dec = strlen($number) - ($last_dec_point + 1); | |
$factor = '1' . str_pad('', $dec, '0', STR_PAD_RIGHT); | |
return $prefix . number_format(preg_replace('/[^0-9\-]/', '', $number) / $factor , $decimals, $dec_point, $thousands_sep); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
N I C E