Search code examples
phpstringescapinghtml-entitiesmoney-format

How to replace * with ✪ ("&\#x272A;") in `money_format`?


setlocale(LC_MONETARY, 'en_US');
$str = money_format('%=*#4.4n',163.17852837291);

returns $**163.1785 for $str.

I'd like to print $✪✪163.1785, instead.

How do I solve the problem?


Solution

  • money_format can only accept a single byte fill character, so you cannot directly achieve what you want. However you can use str_replace after money_format to change the * into :

    setlocale(LC_MONETARY, 'en_US');
    $str = money_format('%=*#4.4n',163.17852837291);
    $str = str_replace('*', '✪', $str);
    echo $str;
    

    Output:

    ✪163.1785
    

    Note there's only one as you have specified a width of 4 and you have 3 digits in the number.

    Demo on 3v4l.org