如何检查字符串是否为int,而不是double等?

Ror*_*ory 141 php string int types casting

PHP有一个intval()将字符串转换为整数的函数.但是,我想事先检查字符串是否为整数,以便我可以向用户提供有用的错误消息,如果它是错误的.PHP有is_int(),但是对于字符串,它返回false "2".

PHP具有该is_numeric()函数,但如果该数字为double,则返回true.我想要一个会为double返回false的东西,但是对于int来说是真的.

例如:

my_is_int("2") == TRUE
my_is_int("2.1") == FALSE
Run Code Online (Sandbox Code Playgroud)

Dom*_*ger 167

怎么用ctype_digit

从手册:

<?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";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

上面的例子将输出:

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.

这仅在输入始终为字符串时才有效:

$numeric_string = '42';
$integer        = 42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false
Run Code Online (Sandbox Code Playgroud)

如果您的输入可能是类型int,那么结合ctype_digit使用is_int.

如果你关心负数,那么你就需要检查输入的前面-,如果是这样,调用ctype_digitsubstr的输入字符串.像这样的东西会这样做:

function my_is_int($input) {
  if ($input[0] == '-') {
    return ctype_digit(substr($input, 1));
  }
  return ctype_digit($input);
}
Run Code Online (Sandbox Code Playgroud)

  • 负数? (6认同)
  • 如果你想检查负整数,添加使用**abs()**就像`ctype_digit(abs($ str))`而不只是`ctype_digit`. (4认同)
  • @enchance`abs('definitelynotanint')=== 0` 而且,`ctype_digit`只接受字符串 (3认同)
  • @Anurag,编辑(尽管如果你真的关心这个东西,正则表达式可能更简单)。 (2认同)

Gor*_*don 80

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如果您只想将布尔值作为返回值,请将其包装到函数中,例如

function validatesAsInt($number)
{
    $number = filter_var($number, FILTER_VALIDATE_INT);
    return ($number !== FALSE);
}
Run Code Online (Sandbox Code Playgroud)

  • @grenoult https://3v4l.org/qVRRS为字符串和整数提供int 0,所以不确定为什么你认为它不起作用.你是否将结果与`==`进行比较? (6认同)
  • 我喜欢大多数答案,但我觉得这个答案最优雅. (2认同)

nic*_*ckf 19

给Dominic的答案+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
Run Code Online (Sandbox Code Playgroud)

  • 此方法不适用于非数字字符串:is_int("abc"+0)为true (6认同)
  • 也给你+1(虽然我想知道通过修剪输入字符串是否能更清楚地处理这个特殊情况). (2认同)

小智 13

将它转换为int.如果它的int值仍然相同;

function my_is_int($var) {
  $tmp = (int) $var;
  if($tmp == $var)
       return true;
  else
       return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 甚至更短:`return $ var ==(int)$ var;` (2认同)
  • @AI.G。如果演员失败会发生什么 (2认同)
  • 这非常接近,但如果不需要的话,应该避免类型杂乱。如果 $var 是“123xxxx”,这将起作用。 (2认同)

Jon*_*gne 8

我喜欢使用的一种非常干净的方式就是那个。你投了两次,第一次在int,第二次在string,然后你严格比较===。请参阅下面的示例:

$value === (string)(int)$value;
Run Code Online (Sandbox Code Playgroud)

现在,关于您的函数 my_is_int,您可以执行以下操作:

function my_is_int($value){ return $value === (string)(int)$value; }
my_is_int('2');  // true
my_is_int('2.1') // false
Run Code Online (Sandbox Code Playgroud)


Gmo*_*onC 7

/**
 * 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

来源.


Cos*_*sta 6

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
Run Code Online (Sandbox Code Playgroud)


在PHP文档注释中遇到了这个片段,在测试之后,它几乎涵盖你抛出的所有内容:

function my_is_int($s) {
    return (is_numeric($s) ? intval($s) == $s : false);
}
Run Code Online (Sandbox Code Playgroud)


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
Run Code Online (Sandbox Code Playgroud)


但要小心:

my_is_int(2.0); //true
my_is_int("2.0"); //true
Run Code Online (Sandbox Code Playgroud)


小智 5

function my_is_int($var) {
    return preg_match('/^\d+$/', $var);
}
Run Code Online (Sandbox Code Playgroud)

  • 我使用了它的变体,允许数字信号:`'/^[-+]?[0-9]+$/'` (2认同)

vie*_*tbq 5

它工作完美!希望对你有帮助=)

$value = 1; // true
$value = '1'; // true
$value = '1.0'; // true
$value = ' 1'; // true
$value = '01'; // true

$value = -1; // true
$value = '-1'; // true
$value = '-1.0'; // true
$value = ' -1'; // true
$value = '-01'; // true

$value = PHP_INT_MIN; // true
$value = PHP_INT_MAX; // true
$value = 0x000001; // true

$value = 'a'; // false
$value = '1a'; // false
$value = 'a1'; // false
$value = 1.1; // false
$value = '1.1'; // false
$value = '--1'; // false
$value = []; // false
$value = [1,2]; // false
$value = true; // false
$value = false; // false
$value = null; // false
$value = 'NaN'; // false
$value = 'undefined'; // false

function isInt($value) {
    return is_numeric($value) && floatval(intval($value)) === floatval($value);
}
Run Code Online (Sandbox Code Playgroud)