Page 1 of 1

How to strip variable and add comas where needed?

Posted: Thu Nov 17, 2011 4:46 pm
by tech0925
I was wondering if someone could tell me how to properly strip and add comas to numbers. I currently have numbers being entered into my database without comas.

Can someone show me a good way to make sure that the info being entered in is a number and how to strip the variable leaving only numbers. Then properly adding comas where they should be. I am referring to the money system. Here are some possible results:

12,583,573
or
22,684,593.85
or
1,348.21

Not sure how to google this one. Thanks!

Re: How to strip variable and add comas where needed?

Posted: Thu Nov 17, 2011 5:35 pm
by Celauran
Strip everything but digits and decimals:

Code: Select all

$input = preg_replace( '/[^\d\.]/', '', $input );
For formatting, check number_format() or, perhaps better, money_format()

Re: How to strip variable and add comas where needed?

Posted: Thu Nov 17, 2011 5:41 pm
by tech0925
Oh ok, thanks once again 4 your help!!

Cheers!