note 81901 added to function.money-format
| From: | RafaelM dot Salvioni at osu1 dot php dot net | Date: | Tue, 18 Mar 2008 21:31:59 +0000 |
| Subject: | note 81901 added to function.money-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137183@lists.php.net to get a copy of this message | ||
<?php
/*
That it is an implementation of the function money_format for the
platforms that do not it bear.
The function accepts to same string of format accepts for the
original function of the PHP.
(Sorry. my writing in English is very bad)
The function is tested using PHP 5.1.4 in Windows XP
and Apache WebServer.
*/
if (!function_exists('money_format')) {
function money_format($format, $number)
{
$regex = array(
'/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?',
'(?:\.([0-9]+))?([in%])/'
);
$regex = implode('', $regex);
if (setlocale(LC_MONETARY, null) == '') {
setlocale(LC_MONETARY, '');
}
$locale = localeconv();
$number = floatval($number);
if (!preg_match($regex, $format, $fmatch)) {
trigger_error("No format specified or invalid format",
E_USER_WARNING);
return $number;
}
$flags = array(
'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
$match[1] : ' ',
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
$match[0] : '+',
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
);
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
$right = trim($fmatch[4]) ? (int)$fmatch[4] :
$locale['int_frac_digits'];
$conversion = $fmatch[5];
$positive = true;
if ($number < 0) {
$positive = false;
$number *= -1;
}
$letter = $positive ? 'p' : 'n';
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
if (!$positive) {
$signal = $locale['negative_sign'];
switch (true) {
case $locale['n_sign_posn'] == 0 || $flags['signal'] ==
'(':
$prefix = '(';
$suffix = ')';
break;
case $locale['n_sign_posn'] == 1:
$prefix = $signal;
break;
case $locale['n_sign_posn'] == 2:
$suffix = $signal;
break;
case $locale['n_sign_posn'] == 3:
$cprefix = $signal;
break;
case $locale['n_sign_posn'] == 4:
$csuffix = $signal;
break;
}
}
if (!$flags['nosimbol']) {
$currency = $cprefix;
$currency .= (
$conversion == 'i' ?
$locale['int_curr_symbol'] :
$locale['currency_symbol']
);
$currency .= $csuffix;
} else {
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
$number = number_format($number, $right,
$locale['mon_decimal_point'],
$flags['nogroup'] ? '' :
$locale['mon_thousands_sep']
);
$number = explode($locale['mon_decimal_point'], $number);
$n = strlen($prefix) + strlen($currency);
if ($left > 0 && $left > $n) {
if ($flags['isleft']) {
$number[0] .= str_repeat($flags['fillchar'], $left - $n);
} else {
$number[0] = str_repeat($flags['fillchar'], $left - $n) .
$number[0];
}
}
$number = implode($locale['mon_decimal_point'], $number);
if ($locale["{$letter}_cs_precedes"]) {
$number = $prefix . $currency . $space . $number . $suffix;
} else {
$number = $prefix . $number . $space . $currency . $suffix;
}
if ($width > 0) {
$number = str_pad($number, $width, $flags['fillchar'],
$flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT);
}
$format = str_replace($fmatch[0], $number, $format);
return $format;
}
}
?>
----
Server IP: 200.185.126.110
Probable Submitter: 200.162.225.69
----
Manual Page -- http://www.php.net/manual/en/function.money-format.php
Edit -- https://master.php.net/note/edit/81901
Del: integrated -- https://master.php.net/note/delete/81901/integrated
Del: useless -- https://master.php.net/note/delete/81901/useless
Del: bad code -- https://master.php.net/note/delete/81901/bad+code
Del: spam -- https://master.php.net/note/delete/81901/spam
Del: non-english -- https://master.php.net/note/delete/81901/non-english
Del: in docs -- https://master.php.net/note/delete/81901/in+docs
Del: other reasons-- https://master.php.net/note/delete/81901
Reject -- https://master.php.net/note/reject/81901
Search -- https://master.php.net/manage/user-notes.php