note 39078 added to function.is-numeric
| From: | joe at kewlmail dot net | Date: | Fri, 16 Jan 2004 08:12:18 +0000 |
| Subject: | note 39078 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63671@lists.php.net to get a copy of this message | ||
Here is a simple function that I found usefull for filtering user input into numbers. Basically, it
attempts to fix fat fingering. For example:
$userString = "$654.4r5";
function numpass_filter($userString){
$money = explode(".", $userString);
//now $money[0] = "$645" and $money[1] = "4r5"
//next remove all characters save 0 though 9
//in both elements of the array
$dollars = eregi_replace("[^0-9]", null, $money[0]);
$cents = eregi_replace("[^0-9]", null, $money[1]);
//if there was a decimal in the original string, put it back
if((string)$cents!=null){
$cents = "." . $cents;
}
$result = $dollars . $cents;
return($result);
}
The output in this case would be '654.45'.
Please note that this function will work properly unless the user fat fingers an extra decimal in
the wrong place.
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39078
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39078&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39078&report=yes
Search -- http://master.php.net/manage/user-notes.php