php php验证整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4100022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php validate integer
提问by omtr
I`m wonder why this not working
我想知道为什么这不起作用
echo gettype($_GET['id']); //returns string
if(is_int($_GET['id']))
{
echo 'Integer';
}
How to validate data passing from GET/POST if it is integer ?
如果数据是整数,如何验证从 GET/POST 传递的数据?
回答by Gordon
Can use
可以使用
$validatedValue = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
See http://php.net/filter_inputand related functions.
参见http://php.net/filter_input和相关函数。
回答by codaddict
The manualsays:
该手册说:
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
要测试变量是数字还是数字字符串(例如表单输入,它始终是字符串),必须使用 is_numeric()。
Alternative you can use the regex based test as:
或者,您可以使用基于正则表达式的测试作为:
if(preg_match('/^\d+$/',$_GET['id'])) {
// valid input.
} else {
// invalid input.
}
回答by eiffel81
To validate form data (string) as an integer, you should use ctype_digit()
It returns TRUE
if every character in the string text is a decimal digit, FALSE
otherwise.
(PHP 4 >= 4.0.4, PHP 5)
要将表单数据(字符串)验证为整数,如果字符串文本中的每个字符都是十进制数字,则应使用ctype_digit()
It 返回TRUE
,FALSE
否则。(PHP 4 >= 4.0.4,PHP 5)
Reference: http://php.net/manual/en/function.ctype-digit.php
回答by Phill Pafford
Try:
尝试:
if(isNumeric($_GET['id'])) {
$cast_int = (int)$_GET['id'];
}
if(isset($cast_int)) {
echo gettype($cast_int)."<br />\n";
if(is_int($cast_int))
{
echo 'Integer'."<br />\n";
}
} else {
echo gettype($_GET['id'])." was passed<br />\n";
}
function isNumeric($numeric) {
return preg_match("/^[0-9]+$/", $numeric);
}
回答by DeviousBlue
It sounds like you are checking if a string contains an integer, rather than if that variable isan integer. If so, you should check out php's regex (regular expression) functionality. It allows you to check for very specific patterns in a string to validate it for whatever criteria. (such as if it contains only number characters)
听起来您正在检查字符串是否包含整数,而不是该变量是否为整数。如果是这样,您应该查看 php 的 regex(正则表达式)功能。它允许您检查字符串中非常特定的模式,以根据任何标准对其进行验证。(例如如果它只包含数字字符)
Here's the php page http://php.net/manual/en/function.preg-match.php
这是 php 页面 http://php.net/manual/en/function.preg-match.php
and here's a cheat sheet on regular expressions (to make the $pattern string) http://regexpr.com/cheatsheet/
这是关于正则表达式的备忘单(用于制作 $pattern 字符串) http://regexpr.com/cheatsheet/
回答by arbithero
I take a slightly more paranoid approach to sanitizing GET input
我采取了一种稍微偏执的方法来清理 GET 输入
function sanitize_int($integer, $min='', $max='')
{
$int = intval($integer);
if((($min != '') && ($int < $min)) || (($max != '') && ($int > $max)))
return FALSE;
return $int;
}
To be even safer, you can extract only numbers first and then run the function above
为了更安全,您可以先只提取数字,然后运行上面的函数
function sanitize_paranoid_string($string, $min='', $max='')
{
$string = preg_replace("/[^a-zA-Z0-9]/", "", $string);
$len = strlen($string);
if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
return FALSE;
return $string;
}
Code from: http://libox.net
代码来自:http: //libox.net