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_digit上substr的输入字符串.像这样的东西会这样做:
function my_is_int($input) {
if ($input[0] == '-') {
return ctype_digit(substr($input, 1));
}
return ctype_digit($input);
}
Run Code Online (Sandbox Code Playgroud)
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)
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)
小智 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)
我喜欢使用的一种非常干净的方式就是那个。你投了两次,第一次在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)
/**
* 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)
来源.
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)
它工作完美!希望对你有帮助=)
$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)
| 归档时间: |
|
| 查看次数: |
79179 次 |
| 最近记录: |