note 34493 deleted from language.types.integer by bjori
| From: | bjori@php.net | Date: | Sat, 25 Mar 2006 21:42:03 +0000 |
| Subject: | note 34493 deleted from language.types.integer by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106607@lists.php.net to get a copy of this message | ||
Note Submitter: Matt AKA junkie
----
Let's say we're in the situation, let's say a from a form, and the what you want from
them is an integer. To be nice though, we're going to fix their error of adding anything other
than an integer instead of telling them to do it.
function make_int($str) {
# Let's make sure this isn't being done for no reason
if (gettype($str)!="integer") {
# First we'll make it into a "float" (physically speaking) if not already one
$i = preg_replace("/[^0-9.]/", "", $str);
# Then we make it an integer physically
$i = preg_replace("/\.[0-9]*$/", "", $i);
# Finally, we define the integer as an integer
$i = (int) $i;
return $i;
} else {
return $str;
}}
Just to reiterate, this will take the "string" (not the type) inputted, and make it an
integer.