-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On September 11, 2002 16:31, Jason Caldwell wrote:
> I need to extract the numbers only from a field.
>
> For example: I have an AMOUNT field and so that I can filter out any user
> typo's I would like to extract the numbers only.
>
> If the user enters $56.55 for example or just $56 then I would like to be
> able to remove the "$" and the "." keeping just the 56 or 5655.
>
> Can I use eregi_replace() to do this -- I've been trying but it doesn't
> seem to work right.
Hey Jason,
You can strip out all non-numeric values with this regular expression:
$v = ereg_replace('[^0-9]', '', $v);
If you want to keep the decimal place, then use this
$v = ereg_replace('[^.0-9]', '', $v);
And finally, if you want a regular expression that recognizes monetary
values, try this :)
$regex = '^' # match the start of a string
. '(' # capture any text that matches the regex
. '[^0-9]*' # match any character that is not 0-9
. ')' # stop capturing text
# Capture match so that we can see if
# they used a currency symbol :)
. '?' # Optionally match the preceding bracketed text
. '(' # capture any text that matches the regex
. '[0-9]+' # match an integer number
.'|[,.][0-9]{0,2}' # or match a decimal value
.'|[0-9]+[,.][0-9]{0,2}' # match integer and decimal
.')' # Stop capturing text
.'([^0-9]+)' # Capture any trailing text (like USD or CAD)
. '?'; # Optionally match the preceding bracketed text
if( ereg($regex, $possible_numeric_value, $captured_text) ) {
echo "$possible_numeric_value might be a monetary value.";
var_dump ($captured_text);
} else {
echo "$possible_numeric_value is probably not a monetary value.";
}
Cheers!
- --zak
Cheers!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9f89Cb6QONwsK8bIRAsP4AJ9X6RqWUDJYtagcJvZ37f/UoRJaYgCfbg1S
22ioG6nALcALqXd6xjGxOrQ=
=IbP1
-----END PGP SIGNATURE-----
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php