【问题标题】:PHP: Simplest way to add a string to a position in a string?PHP:将字符串添加到字符串中的位置的最简单方法?
【发布时间】:2011-01-12 15:56:18
【问题描述】:

我知道我可以编写一个自定义函数来执行此操作,但感觉这是一个应该已经存在(内置)的函数。

所以,我只是想添加空格以使其更易于阅读:

1200 将变为:1 200 10000 将变为:10 000 150000 将变为:150 000 等等……

是否有一个函数可以将一个字符串/字符“弹出”到另一个字符串的指定位置?

【问题讨论】:

    标签: php string position add


    【解决方案1】:

    您只是在格式化数字吗? number_format() 可以接受任何类型的分隔符:

    $formatted = number_format(1200, 0, '.', ' '); // "1 200"
                                     1   2    3
    1. # of decimal places
    2. decimal place character (not inserted, since we've specified 0 decimals)
    3. thousands separator (space, in this case)
    

    【讨论】:

    • 太完美了,甚至比我想要的功能还要好。谢谢,伙计!
    猜你喜欢
    • 2011-01-03
    • 2011-10-16
    • 2014-07-17
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2020-02-26
    相关资源
    最近更新 更多