Topic: php 7.4 function money_format() is deprecated

line 776 of pdf_report.inc uses function money_format() that is deprecated in php7.4

Phuong

Re: php 7.4 function money_format() is deprecated

We could use the NumberFormatter instead, but it is handling it differently. I wonder how important this is?
We could simply skip the currency formatter, I guess. What do you guys think? Right now it is not used seriously. We could use the number_format2, like we do normally.

/Joe

Re: php 7.4 function money_format() is deprecated

@joe, as a workaround this is what I did for another website that needed the  money_format() function.

A proper refactoring to a supported library/function would be preferred, but this works as an interim solution.

Hope this helps

money_format.php

<?php

/**
* Implementation of money_format function for platforms that do not
* implictly support it
*
*
* @link       paulshipley.com.au
* @since      1.0.0
* @see        https://www.php.net/manual/en/function.money-format.php
*
* @package    Winesbydesign
* @subpackage Winesbydesign/includes
*/

if (!function_exists('money_format')) {
    /*
    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.
    */
    function money_format($format, $number)
    {
        $regex  = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
        '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
        if (setlocale(LC_MONETARY, 0) == 'C') {
            setlocale(LC_MONETARY, '');
        }
        $locale = localeconv();
        preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
        foreach ($matches as $fmatch) {
            $value = floatval($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 ($value < 0) {
                $positive = false;
                $value  *= -1;
            }
            $letter = $positive ? 'p' : 'n';

            $prefix = $suffix = $cprefix = $csuffix = $signal = '';

            $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
            switch (true) {
                case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
                    $prefix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
                    $suffix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
                    $cprefix = $signal;
                    break;
                case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
                    $csuffix = $signal;
                    break;
                case $flags['usesignal'] == '(':
                case $locale["{$letter}_sign_posn"] == 0:
                    $prefix = '(';
                    $suffix = ')';
                    break;
            }
            if (!$flags['nosimbol']) {
                $currency = $cprefix .
                ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
                $csuffix;
            } else {
                $currency = '';
            }
            $space  = $locale["{$letter}_sep_by_space"] ? ' ' : '';

            $value = number_format($value, $right, $locale['mon_decimal_point'],
            $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
            $value = @explode($locale['mon_decimal_point'], $value);

            $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
            if ($left > 0 && $left > $n) {
                $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
            }
            $value = implode($locale['mon_decimal_point'], $value);
            if ($locale["{$letter}_cs_precedes"]) {
                $value = $prefix . $currency . $space . $value . $suffix;
            } else {
                $value = $prefix . $value . $space . $currency . $suffix;
            }
            if ($width > 0) {
                $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
                STR_PAD_RIGHT : STR_PAD_LEFT);
            }

            $format = str_replace($fmatch[0], $value, $format);
        }
        return $format;
    }
}

Re: php 7.4 function money_format() is deprecated

Attachment is the code in file format.

Post's attachments

money_format.zip 1.5 kb, 2 downloads since 2021-03-08 

You don't have the permssions to download the attachments of this post.

Re: php 7.4 function money_format() is deprecated

Hello again,

I see that we already have a money_format that has been used for OS Win in /includes/current_user.inc on line 369:

// function money_format doesn't exist in OS Win.
if (!function_exists('money_format'))
{
    function money_format($format, $number) 
    {
        return price_format($number);
    } 
}    

All windows users have been using this routine for years without problems. Maybe we should include the function here suggested by @PaulSipley instead. I see that this function is listed in PHP.NET as an alternative to money_format. There is also another slightly better version we can use.

What do you think?

/Joe