Stripping data from a varchar string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Stripping data from a varchar string

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

str_replace() is your friend.
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Post 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?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

tpra21 wrote: will it automatically delete the comma if I don't specify a replacement character in the function?
yep.
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Post by tpra21 »

Thanks a million!!!
Post Reply