php 如何检查字符串是否为 int,而不是 double 等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2012187/
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
How to check that a string is an int, but not a double, etc.?
提问by Rory
PHP has an intval()function that will convert a string to an integer. However I want to check that the string is an integer beforehand, so that I can give a helpful error message to the user if it's wrong. PHP has is_int(), but that returns false for string like "2".
PHP 有一个intval()函数可以将字符串转换为整数。但是,我想事先检查字符串是否为整数,以便在错误时向用户提供有用的错误消息。PHP 有is_int(),但是对于像"2".
PHP has the is_numeric()function, but that will return true if the number is a double. I want something that will return false for a double, but true for an int.
PHP 有这个is_numeric()函数,但如果数字是双精度,它会返回 true。我想要一些对于双精度返回 false 而对于 int 返回 true 的东西。
e.g.:
例如:
my_is_int("2") == TRUE
my_is_int("2.1") == FALSE
回答by Dominic Rodger
How about using ctype_digit?
怎么用ctype_digit?
From the manual:
从手册:
<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "The string $testcase consists of all digits.\n";
} else {
echo "The string $testcase does not consist of all digits.\n";
}
}
?>
The above example will output:
上面的例子将输出:
The string 1820.20 does not consist of all digits. The string 10002 consists of all digits. The string wsl!12 does not consist of all digits.
This will only work if your input is always a string:
这仅在您的输入始终是字符串时才有效:
$numeric_string = '42';
$integer = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer); // false
If your input might be of type int, then combine ctype_digitwith is_int.
如果您的输入可能是 类型int,则ctype_digit与is_int.
If you care about negative numbers, then you'll need to check the input for a preceding -, and if so, call ctype_digiton a substrof the input string. Something like this would do it:
如果你关心负数,那么你就需要检查输入的前面-,如果是这样,调用ctype_digit上substr的输入字符串。像这样的事情会做到:
function my_is_int($input) {
if ($input[0] == '-') {
return ctype_digit(substr($input, 1));
}
return ctype_digit($input);
}
回答by Gordon
filter_varshould do it:
filter_var应该这样做:
var_dump(filter_var('2', FILTER_VALIDATE_INT)); // 2
var_dump(filter_var('2.0', FILTER_VALIDATE_INT)); // false
var_dump(filter_var('2.1', FILTER_VALIDATE_INT)); // false
but
但
var_dump(filter_var(2, FILTER_VALIDATE_INT)); // 2
var_dump(filter_var(2.0, FILTER_VALIDATE_INT)); // 2
var_dump(filter_var(2.1, FILTER_VALIDATE_INT)); // false
If you just want Booleans as return values, wrap it into a function, e.g.
如果您只想要布尔值作为返回值,请将其包装到一个函数中,例如
function validatesAsInt($number)
{
$number = filter_var($number, FILTER_VALIDATE_INT);
return ($number !== FALSE);
}
回答by nickf
+1 to Dominic's answer (using ctype_digit). Another way you could do it is with type coercion:
+1 多米尼克的回答(使用ctype_digit)。另一种方法是使用类型强制:
$inty = "2";
$inty2 = " 2";
$floaty = "2.1";
$floaty2 = "2.0";
is_int($inty + 0); // true
is_int($floaty + 0); // false
is_int($floaty2 + 0); // false
// here's difference between this and the ctype functions.
is_int($inty2 + 0); // true
ctype_digit($inty2); // false
回答by greg
Cast it to int. if it still have the same value its int;
将其转换为 int。如果它仍然具有相同的值,则它的 int 值;
function my_is_int($var) {
$tmp = (int) $var;
if($tmp == $var)
return true;
else
return false;
}
回答by GmonC
/**
* Check if a number is a counting number by checking if it
* is an integer primitive type, or if the string represents
* an integer as a string
*/
function is_int_val($data) {
if (is_int($data) === true) return true;
if (is_string($data) === true && is_numeric($data) === true) {
return (strpos($data, '.') === false);
}
}
来源。
回答by Costa
Had a need for a robust is_intrecently. I found intval() too unpredictable:
is_int最近需要一个健壮的。我发现 intval() 太不可预测了:
intval(array('foo', 'bar')) //returns 1 ?!?
intval("2dog") //returns 2 even though the value is definitely not an integer
intval("dog2") //also returns 2
Came across this snippet in the PHP documentation comments, and after testing it, it covers almosteverything you throw at it:
在 PHP 文档注释中发现了这个片段,经过测试,它几乎涵盖了你扔给它的所有内容:
function my_is_int($s) {
return (is_numeric($s) ? intval($s) == $s : false);
}
my_is_int(2); //true
my_is_int("2"); //true
my_is_int(2.1); //false
my_is_int("2.1"); //false
my_is_int("dog"); //false
my_is_int("2dog"); //false
my_is_int("dog2"); //false
my_is_int(array('foo', 'bar')); //false
my_is_int(array(1)); //false
But careful:
但要小心:
my_is_int(2.0); //true
my_is_int("2.0"); //true
回答by Jonathan Gagne
One really clean way that I like to use is that one. You cast it twice, first in int, secondly in string, then you strict compare ===. See the example below:
我喜欢使用的一种非常干净的方式就是那个。你投了两次,第一次在int,第二次在string,然后你严格比较===。请参阅下面的示例:
$value === (string)(int)$value;
Now, about your function my_is_int, you can do something like this:
现在,关于您的函数 my_is_int,您可以执行以下操作:
function my_is_int($value){ return $value === (string)(int)$value; }
my_is_int('2'); // true
my_is_int('2.1') // false
回答by Michael Connor
function my_is_int($var) {
return preg_match('/^\d+$/', $var);
}
回答by julesdude
I′m using this one:
我正在使用这个:
function isInt($val){
return (filter_var($val, FILTER_VALIDATE_INT) !== false && strpos($val, '-') === false);
}
var_dump (isInt("1"));
回答by Jordi Martínez
You can just check for a number, if it is then check than casting is given a double or not:
您可以只检查一个数字,如果是,则检查比铸造是否给双:
((is_numeric($var) && !is_double(1*$var)));
Just for positive numbers:
仅适用于正数:
(is_numeric($var) && !is_double(1*$var)) && ($var >= 0)
Checking it:
检查它:
$numbersToCheck = array("a", "-1", "1", "1.0", "1.2");
foreach ($numbersToCheck as $var) {
echo $var . " is integer? ";var_dump((is_numeric($var) && !is_double(1*$var)));
echo $var . " is a positive integer? ";var_dump((is_numeric($var) && !is_double(1*$var)) && ($var >= 0));
}
Output:
输出:
a is integer? bool(false)
a is a positive integer? bool(false)
-1 is integer? bool(true)
-1 is a positive integer? bool(false)
1 is integer? bool(true)
1 is a positive integer? bool(true)
1.0 is integer? bool(false)
1.0 is a positive integer? bool(false)
1.2 is integer? bool(false)
1.2 is a positive integer? bool(false)

