php 如何检查PHP变量是否包含非数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6290316/
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 if PHP variable contains non-numbers?
提问by user701510
I just want to know the method to check a PHP variable for any non-numbers and if it also detects spaces between characters? Need to make sure nothing weird gets put into my form fields. Thanks in advance.
我只想知道检查任何非数字的 PHP 变量的方法,以及它是否还检测字符之间的空格?需要确保我的表单字段中没有任何奇怪的东西。提前致谢。
回答by Marty
If you mean that you only want a value to contain digits then you can use ctype_digit()
.
如果你的意思是你只想要一个包含数字的值,那么你可以使用ctype_digit()
.
回答by Matthieu Napoli
You can use is_numeric()
:
您可以使用is_numeric()
:
if ( is_numeric($_POST['foo']) ) {
$foo = $_POST['foo'];
} else {
//?Error
}
This will check that the value is numerical, so it may contain something else than digits:
这将检查该值是否为数字,因此它可能包含数字以外的其他内容:
12
-12
12.1
But this will ensure that the value is a valid number.
但这将确保该值是一个有效的数字。
回答by nickf
You can use ctype_digit
您可以使用 ctype_digit
eg:
例如:
if (!ctype_digit($myString)) {
echo "Contains non-numbers.";
}
回答by SteeveDroz
This will return true
if there are non-numbers in the string. It detects letters, spaces, tabs, new lines, whatever isn't numbers.
true
如果字符串中有非数字,这将返回。它检测字母、空格、制表符、换行符,以及任何不是数字的东西。
preg_match('#[^0-9]#',$variable)
回答by Amit Mhaske
This will check whether the input value is numeric or not. Hope this helps
这将检查输入值是否为数字。希望这可以帮助
if(!preg_match('#[^0-9]#',$value))
{
echo "Value is numeric";
}
else
{
echo "Value not numeric";
}
回答by Ed Brown
PHP has the function is_numeric()which may be what you are looking for.
PHP 具有函数is_numeric(),这可能是您正在寻找的。
回答by symcbean
Cast and compare:
投射和比较:
function string_contain_number($val)
{
return ($val + 0 == $val) ? true : false;
}
回答by Luca C.
if(!ctype_digit($string))
echo 'The string contains some non-digit'
回答by AmirHossein
Assuming you want only (and only) valid integers and you dont want users to mess up with your data and database with hexadecimal
or binary
or any other form of numbers then you can always use this method:
假设您只需要(并且只需要)有效整数,并且您不希望用户使用hexadecimal
或binary
或任何其他形式的数字来弄乱您的数据和数据库,那么您始终可以使用此方法:
if(((string) (int) $stringVariable) === $stringVariable) {
// Thats a valid integer :p
}
else {
// you are out of luck :(
}
The trick is simple. It casts the variable of type string to integer type and then casts it back to string. Super fast, Super simple.
诀窍很简单。它将字符串类型的变量转换为整数类型,然后将其转换回字符串。超级快,超级简单。
For the sake of testing I've prepared a test:
为了测试,我准备了一个测试:
'1337' is pure integer.
'0x539' is not.
'02471' is not.
'0000343' is not.
'0b10100111001' is not.
'1337e0' is not.
'not numeric' is not.
'not numeric 23' is not.
'9.1' is not.
'+655' is not.
'-586' is pure integer.
The only place that this method falls short is on negative numbers, so you need to check that beside this (use ((string) (int) $stringVariable) === $stringVariable && $stringVariable[0] !== "-"
).
此方法唯一不足的地方是负数,因此您需要在此旁边检查(使用((string) (int) $stringVariable) === $stringVariable && $stringVariable[0] !== "-"
)。
Now I thought that the preg_match
method is the best. but in any project that deals with users, speed is an important factor. so I prepared a benchmarking test doing that above test for 500,000 times and the results were amazing:
现在我认为这个preg_match
方法是最好的。但在任何与用户打交道的项目中,速度都是一个重要因素。所以我准备了一个基准测试,上面的测试做了 500,000 次,结果非常惊人:
My own invented method took only:6.4700090885162
seconds to complete as compared to preg_match
which took:77.020107984543
seconds to complete this test!
我自己发明的方法只需要:6.4700090885162
几秒钟即可完成,而完成此测试所需的preg_match
时间:77.020107984543
几秒钟!