note 39078 deleted from function.is-numeric by crell
| From: | crell@php.net | Date: | Mon, 18 Apr 2022 14:57:05 +0000 |
| Subject: | note 39078 deleted from function.is-numeric by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218279@lists.php.net to get a copy of this message | ||
Note Submitter: joe at kewlmail dot net
----
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.