Sanitizing Numbers
| From: | Matt Neimeyer | Date: | Fri, 13 Mar 2009 17:34:14 +0000 |
| Subject: | Sanitizing Numbers | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-290007@lists.php.net to get a copy of this message | ||
I'm trying to sanitize some numeric data that's coming to us from
another system which I have no control over where all fields are
character fields with no formatting from the end user so data is a
mishmash of clean and mixed types of dirty.
I know I can use intval and floatval to sanitize if the numeric data
is at the front of the string but what about when it's not?
For example, Jersey Number = #45 or Dues = $1,234.56....
I see in the comments at php.net for floatval a lot of very complex
solutions... am I missing something about the following that wouldn't
cover me?
<?php $output = floatval(ereg_replace("[^-0-9\.]","",$input)); ?>
I'm willing to assuming only US formatted numbers... and knowing that
if they put in 45/46 for jersey it would come out 4546 (but I might
put in additional code for that specific case on that specific
field...). I'm also looking for something that I can generically apply
to any numeric field.
Thanks for your collective guidance.
Matt