php 检查字符串是否包含整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3377537/
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
Checking if a string contains an integer
提问by Emanuil Rusev
Do you know of a function that can check if a string contains an integer?
你知道一个可以检查字符串是否包含整数的函数吗?
Here's how I'd expect it to work:
这是我期望它的工作方式:
holds_int("23") // should return true.
holds_int("2.3") // should return false.
holds_int("qwe") // should return false.
回答by Jhong
if((string)(int)$var == $var) {
echo 'var is an integer or a string representation of an integer';
}
Example results:
结果示例:
var_dump( test(1) ); // TRUE
var_dump( test('1') ); // TRUE
var_dump( test('1.0') ); // TRUE
var_dump( test('1.1') ); // false
var_dump( test('0xFF') ); // false
var_dump( test('0123') ); // TRUE
var_dump( test('01090') ); // TRUE
var_dump( test('-1000000') ); // TRUE
var_dump( test('+1000000') ); // TRUE
var_dump( test('2147483648') ); // false
var_dump( test('-2147483649') ); // false
See Gordon's answer below for how this would behave differently if ===were used for comparison instead of ==.
请参阅下面戈登的回答,了解如果===用于比较而不是==.
回答by Ja?ck
Not the fastest method, but filter_var()is quite accurate:
不是最快的方法,但filter_var()非常准确:
function test($s)
{
return filter_var($s, FILTER_VALIDATE_INT) !== false;
}
Here are the results based on Jhong's answer, differences marked with !!:
以下是基于 Jhong 的回答的结果,不同之处标有!!:
var_dump(test(1) ); // true
var_dump(test('1') ); // true
var_dump(test('1.0') ); // false !!
var_dump(test('1.1') ); // false
var_dump(test('0xFF') ); // false
var_dump(test('0123') ); // false !!
var_dump(test('01090') ); // false !!
var_dump(test('-1000000') ); // true
var_dump(test('+1000000') ); // true
var_dump(test('2147483648') ); // true !! on 64bit
var_dump(test('-2147483649')); // true !! on 64bit
To allow octal integers:
允许八进制整数:
function test($s)
{
return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false;
}
Results:
结果:
var_dump(test('0123') ); // true
var_dump(test('01090')); // false !!
To allow hexadecimal notation:
允许使用十六进制表示法:
function test($s)
{
return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX) !== false;
}
Results:
结果:
var_dump(test('0xFF')); // true !!
回答by Gordon
Dont want to accidently turn Jhong's answer into a CW, so for the record here is the results when testing with ===instead of ==.
不想意外地将 Jhong 的答案变成 CW,所以这里的记录是使用===而不是测试时的结果==。
function test($var) {
return ((string)(int)$var === $var);
}
var_dump( test(1) ); // returns false vs TRUE
var_dump( test('1') ); // returns TRUE
var_dump( test('1.0') ); // returns false vs TRUE
var_dump( test('1.1') ); // returns false
var_dump( test('0xFF') ); // returns false
var_dump( test('0123') ); // returns false vs TRUE
var_dump( test('-0123') ); // returns false vs TRUE
var_dump( test('-1000000') ); // returns TRUE
var_dump( test('+1000000') ); // returns false vs TRUE
var_dump( test('2147483648') ); // returns false
var_dump( test('-2147483649') ); // returns false
回答by Darragh Enright
UpdateSince PHP 7.1 there are problems with using
is_int()with non-numeric values, as discussed in this SO Answer. In any case, this is a very old answer and I'd really view it as something of a hack at this point so YMMV ;)
更新自 PHP 7.1 起,使用
is_int()非数字值存在问题,如本SO Answer 中所述。无论如何,这是一个非常古老的答案,在这一点上我真的认为它是一种黑客行为,所以 YMMV ;)
Sorry if this question has been answered but this has worked for me in the past:
对不起,如果这个问题已经得到回答,但这在过去对我有用:
First check if the string is_numeric. if it is add a 0to the value to get PHP to covert the string to its relevant type. Then you can check if it's an int with is_int. Quick and dirty but it works for me...
首先检查字符串is_numeric. 如果0在值中添加 a以使 PHP 将字符串转换为其相关类型。然后你可以检查它是否是一个带有is_int. 又快又脏,但它对我有用......
$values = array(1, '2', '2.5', 'foo', '0xFF', 0xCC, 0644, '0777');
foreach ($values as $value) {
$result = is_numeric($value) && is_int(($value + 0)) ? 'true' : 'false';
echo $value . ': ' . $result . '<br />';
}
Results:
结果:
1: true
2: true
2.5: false
foo: false
0xFF: true
204: true
420: true
0777: true
The only problem is that it will evaluate octal values wrapped in a string literally, i.e: '0123' will simply become 123. But that's easy to address :)
唯一的问题是它会按字面意思计算包裹在字符串中的八进制值,即:'0123' 将简单地变为 123。但这很容易解决 :)
回答by nico
Other option
其他选择
function holds_int($str)
{
return preg_match("/^-?[0-9]+$/", $str);
}
回答by Raza Ahmed
If the string contains spaces, then @Hyman's answer will not provide accurate result. e.g.
如果字符串包含空格,那么@Hyman 的答案将不会提供准确的结果。例如
$var = '19 ';
if((string)(int)$var == $var) {
echo 'var is an integer or a string representation of an integer';
}
The above string will not be an int according to the above check.
根据上面的检查,上面的字符串不会是 int 。
So instead of using this, try following:
因此,不要使用它,请尝试以下操作:
if(ctype_digit(trim('19 '))){
echo 'it is digit ';
}else{
echo 'it is not digit ';
}
回答by Yerke
ctype_digitwill do the trick:
ctype_digit会做的伎俩:
ctype_digit($str)
ctype_digit($str)
$strhas to be a string- extra spaces are not allowed
- no decimal points
.
$str必须是一个字符串- 不允许有多余的空格
- 没有小数点
.
回答by scott
Maybe this will also help in given situation there is a function in php that already does this, its called "is_numeric()" it will return true or false accordenly..
也许这也将有助于在给定的情况下,php 中有一个函数已经这样做了,它被称为“is_numeric()”,它将相应地返回 true 或 false。
if(is_numeric($somestring) == True){
echo "this string contains a integar";
}
link: http://www.php.net/is_numeric
链接:http: //www.php.net/is_numeric
you said "holdsint("2") should return true, well is_numeric("2") returns True, and is_numeric("a") False, as expected, this function exists in php, no need to rewrite.
你说"holdsint("2")应该返回true,那么is_numeric("2")返回True,is_numeric("a") False,正如预期的那样,这个函数存在于php中,不需要重写。
回答by HalfTitle
I liked nyson's suggestion, but noticed that it will be false for '0123'. I'm now doing this:
我喜欢 nyson 的建议,但注意到“0123”是错误的。我现在这样做:
(string)(int)$var === ltrim((string)$var, '0')
(This would have been posted as a comment @nyson, but I don't have enough privileges to do that yet).
(这将作为评论发布@nyson,但我还没有足够的权限这样做)。
Edited to add: If you want zero to be true, you need to do something like
编辑添加:如果您希望零为真,则需要执行以下操作
(int)$var === 0 || (string)(int)$var === ltrim((string)$var, '0')
回答by Sadat
There may be two cases-
可能有两种情况——
You need to check for exact string format of a number(most of ans is about this one)
You want to check, whether a string contains a specific number or not
preg_match('/'.$matching_number.'/',$container_string);
您需要检查数字的确切字符串格式(大部分答案都是关于这个的)
您想检查字符串是否包含特定数字
preg_match('/'.$matching_number.'/',$container_string);

