Page 1 of 1

Strip everything but numbers and dots

Posted: Thu Mar 06, 2008 4:08 pm
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.

Re: Strip everything but numbers and dots

Posted: Thu Mar 06, 2008 4:23 pm
by Christopher

Code: Select all

$price = preg_replace("/[^0-9\.]/", "",$_GET[price]);
Remove everything but numbers and dot.

Re: Strip everything but numbers and dots

Posted: Thu Mar 06, 2008 5:14 pm
by davidtube
Cool. Thanks