Re: number_format problem
| From: | CPT John W. Holmes | Date: | Wed, 05 Nov 2003 16:54:52 +0000 |
| Subject: | Re: number_format problem | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-31433@lists.php.net to get a copy of this message | ||
From: "Dillon, John" <JDillon@cantor.co.uk>
> I want to show a number from a database in the format x,xxx.00 in a
> textfield, then if it is changed by the user I want to post the value of
the
> number to a decimal field. However, once you number_format the number it
> becomes a string, and numbers like 3,379.90 give a value of 3 when posted
to
> the database, which is hinted at in the notes on number_format. I suppose
I
> need a string to number function - can someone tell me what this might be
> called please?
If you know it's going to be in a $x,xxx.xx format, then
$new_number = preg_replace('/[^0-9.]/','',$old_number);
will remove anything that's not a number or decimal point.
BUT, since we know that we can't trust it'll come in that format, you may
also want to run
$rs = preg_match('/[0-9]+\.?([0-9]{1,2})?/',$new_number); //untested :)
which _should_ make sure you're not getting a number like "xxx.xx.xxx" or
"xx.xxxxxx" from some tricky user. If $rs is TRUE or 1, then the number is
in the correct format.
---John Holmes...