Page 1 of 1

Stripping data from a varchar string

Posted: Mon Apr 24, 2006 6:47 pm
by tpra21
I have read much of the PHP function manual, looked through these forums, and apologize if the answer already exists here, but as an avid PHP prorgammer I cannot seem to find the answer to this simple question.

On my website I store all numeric data (totals, monthly rent, etc) in varchar types so that users can enter periods, dollar signs, commas, etc. Now I need to create some reports that uses the numeric (actually varchar/character) data in calculations. How do I strip out the non-numeric positions? The best I have thought up is emploding the thing at the non-numeric characters and then piecing the variables back together.

Example:

how do I get $3,340.21 into --> 3340.21

I know the trim function could probably rid me of the dollar sign, but how do you strip specfic characters out that are within a string.

A million thanks to anyone who can help.

Adam

Posted: Mon Apr 24, 2006 6:51 pm
by n00b Saibot
str_replace() is your friend.

Posted: Mon Apr 24, 2006 7:04 pm
by tpra21
thanks,

but what do I replace the comma with since I just want it stripped and not replaced with anything else? will it automatically delete the comma if I don't specify a replacement character in the function?

Posted: Mon Apr 24, 2006 7:11 pm
by n00b Saibot
tpra21 wrote: will it automatically delete the comma if I don't specify a replacement character in the function?
yep.

Posted: Mon Apr 24, 2006 7:44 pm
by tpra21
Thanks a million!!!