note 34493 added to language.types.integer

From: Date: Tue, 29 Jul 2003 08:46:53 +0000
Subject: note 34493 added to language.types.integer
Groups: php.notes 
Request: Send a blank email to php-notes+get-52888@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.types.integer.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34493 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34493&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34493&report=yes

« previous php.notes (#52888) next »