【问题标题】:How to replace * with ✪ ("&\#x272A;") in `money_format`?如何在`money_format`中用✪(“&\#x272A;”)替换*?
【发布时间】:2019-01-23 22:27:11
【问题描述】:
setlocale(LC_MONETARY, 'en_US');
$str = money_format('%=*#4.4n',163.17852837291);

$str返回$**163.1785

我想打印$✪✪163.1785,而不是。

我该如何解决这个问题?

【问题讨论】:

  • 嗨@Emma,你能用不同的函数代替money_format吗?

标签: php string escaping html-entities money-format


【解决方案1】:

money_format 只能接受单字节填充字符,所以不能直接实现你想要的。但是,您可以在money_format 之后使用str_replace* 更改为

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

输出:

✪163.1785

请注意,只有一个 ,因为您指定的宽度为 4,并且数字中有 3 位数字。

Demo on 3v4l.org

【讨论】:

    【解决方案2】:

    你可以这样定义一个特定的函数:

    function m_money_format($f, $s)
    {
        return str_replace("*", "&\#x272a;", money_format($f,$s));
    }
    
    setlocale(LC_MONETARY, 'en_US');
    $str = m_money_format($f, $s);
    

    因此您可以在需要时在代码的每个部分调用该函数

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 1970-01-01
      • 2011-02-26
      • 2016-08-03
      • 2020-10-23
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多