PHP:通过插入逗号来格式化数字字符串

标签 php regex string formatting

我需要一种通过在合适的地方插入逗号来将数字转换为格式化方式的方法。可以使用正则表达式完成吗?

例子:

12345 => 12,345
1234567 =>1,234,567

最佳答案

不需要 regex ,您可以使用 number_format() 函数轻松完成。

echo number_format(12345); // prints 12,345
echo number_format(1234567); // prints 1,234,567

.

$arr = array(
        1234567890,
        123456789,
        12345678,
        1234567,
        123456,
        12345,
        1234,
        123,
    );

foreach($arr as $num) {
    echo number_format($num)."\n";
}

输出:

1,234,567,890
123,456,789
12,345,678
1,234,567
123,456
12,345
1,234
123

关于PHP:通过插入逗号来格式化数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2437188/

相关文章:

c# - 无法将 '#' 检测为特殊键(Regex Asp.net)

php - 正则表达式:如何在 "@"符号之前删除电子邮件?

php - 将 float 格式化为两位小数

java - Java 正则表达式需要帮助

javascript - 捕获正则表达式灾难性回溯 Node.js

c - 在 C 中搜索文本文件中的特定行

java - 将 [string,string] 转换为数组

php - 在 html 设计中插入页面

php - 使用 .htaccess 设置 upload_max_filesize 不起作用

php - 从html查找所有单词(或句子)