Strip everything but numbers and dots

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
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Strip everything but numbers and dots

Post by davidtube »

I'm making a form which lets the user insert a price in any currency. I want the price stored as a float. How do I strip all characters expect numbers and decimal points?

I have the following code:

Code: Select all

$price = preg_replace("/\D/", "",$_GET[price]);
 
The problem is this also strips the decimal point.

I know how to strip an individual currency symbol, but the users could be using any currency so I'd like to just strip everything that isn't part of the float.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Strip everything but numbers and dots

Post by Christopher »

Code: Select all

$price = preg_replace("/[^0-9\.]/", "",$_GET[price]);
Remove everything but numbers and dot.
(#10850)
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Re: Strip everything but numbers and dots

Post by davidtube »

Cool. Thanks
Post Reply