View Full Version : Insert commas

03-04-2009, 05:19 AM
I try to do my own research before posting. I have a variable that can contain a large string of numbers. I would like commas inserted where needed to make it easier to read.

I found this:

$numbers = implode("",range(1,15));
print "$numbers"; //123456789101112131415
//now insert commas
$newnumbers = substr(chunk_split($numbers,3,","),0,-1);
print "$newnumbers";
//outputs: 123,456,789,101,121,314,15

As you see this works left to right... not right to left.

What is the proper solution?

03-04-2009, 05:55 AM

PHP has a built-in function for inserting decimals and commas. Good luck!

Ranjit Viswakumar
Professional Services Specialist
HostMySite.com (http://hostmysite.com/?utm_source=bb)

03-04-2009, 08:22 AM
Just what I was looking for. Thank you.