note 109750 added to function.intval
| From: | gregrobson at gmail dot com | Date: | Thu, 16 Aug 2012 15:15:28 +0000 |
| Subject: | note 109750 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190102@lists.php.net to get a copy of this message | ||
I just wanted to be sure that a string was going to be valid if I intval'ed it. This regular
expression matches:
0
Any positive integer (no + sign)
Any negative integer (with a - sign)
It does not match a leading 0 or any other character. You are advised to strip out commas, trim the
string and check range yourself ;)
<?php
/**
* Loops through each non-array element and actions a function.
*
* @return boolean|integer TRUE if the value matches as an integer, FALSE if
* an error occured or 0 if not match was found.
*/
public function validate($value)
{
$value = preg_match('/^(0|(-{0,1}[1-9]\d*))$/', trim($value));
if ($value == 1)
return true;
else
return $value;
}
?>
If you use intval() now, you are merely changing the data type.
----
Server IP: 69.147.83.197
Probable Submitter: 82.152.26.217
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- https://master.php.net/note/edit/109750
Del: integrated -- https://master.php.net/note/delete/109750/integrated
Del: useless -- https://master.php.net/note/delete/109750/useless
Del: bad code -- https://master.php.net/note/delete/109750/bad+code
Del: spam -- https://master.php.net/note/delete/109750/spam
Del: non-english -- https://master.php.net/note/delete/109750/non-english
Del: in docs -- https://master.php.net/note/delete/109750/in+docs
Del: other reasons-- https://master.php.net/note/delete/109750
Reject -- https://master.php.net/note/reject/109750
Search -- https://master.php.net/manage/user-notes.php