PHP: 8 ways to convert 1234567890 to 1,234,567,890
Blogs20102010-11-10
Here I write 8 ways to convert 1234567890 to 1,234,567,890, all work fine.
0) { $str = intval($str) / 1000; array\_unshift($a, substr($str, strpos($str,'.')+1, 3)); } if(strlen($a\[count($a)-1\])!=3) { $a\[count($a)-1\] .= '0'; } $s3 = implode(",", $a); $s3 = preg\_replace("/^0+/", '', $s3); echo $s3 . "n"; // 5. **preg\_match** $b = array(); $str = "1234567890"; preg\_match("/(d{1,3})(d{3})(d{3})(d{3})$/", $str, $b); array\_shift($b); $s4 = join(',', $b); echo $s4 . "n"; // 6. **substr\_replace** $str = "1234567890"; $t1 = substr\_replace($str,',',-3,-3); $t2 = substr\_replace($t1,',',-7,-7); $t3 = substr\_replace($t2,',',-11,-11); echo "$t3n"; ?>// 7. money_format
// 8. number_format
