Page 1 of 1
preg_replace get number regardless
Posted: Mon Jan 24, 2005 6:02 pm
by hawleyjr
I have a field in which the user enters in a $ amount. However, if the user enters in $100 or $40.01 or $1,300.00 Instead of throwing an error I would like the value without the $ sign or comma. I know I can do this with str_replace() however, if the user enters "100 dollars" I would still like to process the 100. I know this will involve using reg_replace and that is why I 'm asking for help.
Thanks.
Posted: Mon Jan 24, 2005 6:04 pm
by feyd
Code: Select all
preg_replace('#ї^\d\.]#','',$text);
beware of things like 10.1.1.53 though.. that'd pass untouched.
Posted: Mon Jan 24, 2005 6:19 pm
by hawleyjr
Thanks Feyd,
Out of curiosity is there a benefit to doing the reg_replace vs settype?
http://us4.php.net/manual/en/function.settype.php
Code: Select all
#http://us4.php.net/manual/en/function.settype.php
$foo = "5bar"; // string
settype($foo, "integer"); // $foo is now 5 (integer)
Posted: Mon Jan 24, 2005 6:23 pm
by feyd
if any character other than a number appears in front, it'll always set to zero.