PHP 相当于 JavaScript 的 parseInt 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12650802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:58:12  来源:igfitidea点击:

PHP equivalent of JavaScript's parseInt function?

phpfunctiondigit

提问by Lucas

Possible Duplicate:
Check if a variable is a natural number

可能重复:
检查变量是否为自然数

Just came across where I need to sanitize an input and remove all non-digit characters with PHP. Thought of making a function myself, but just first wanted to check up on the possible built-in functions, so I won't have to spend extra time reinventing the wheel.

刚刚遇到我需要清理输入并使用 PHP 删除所有非数字字符的地方。想自己做一个函数,但首先想检查可能的内置函数,所以我不必花额​​外的时间重新发明轮子。

回答by Spudley

There isn't a direct equivalent to pareseInt()in PHP because PHP doesn't have a NaNdata type. parseInt()will produce NaNif the value can't be parsed.

pareseInt()在 PHP 中没有直接的等价物,因为 PHP 没有NaN数据类型。如果无法解析该值,parseInt()则会产生NaN

In addition, parseInt()has the radix parameter, which none of the PHP options give you.

此外,parseInt()还有 radix 参数,PHP 选项都没有提供该参数。

But that said, if all you want to do is turn a string into an integer, then there are several options:

话虽如此,如果您只想将字符串转换为整数,那么有几种选择:

$int = (int)$string;

$int = intval($string);

$int = settype($string,'integer');

Those three will all work in much the same way. There may be some edge cases where intval()differs from the other two, but in general, they will turn a string into an int. In the absence of NaN, they all output 0(zero) if the string is non numeric and can't be parsed. But this should be sufficient for most DB sanitation purposes.

这三个都将以大致相同的方式工作。可能有一些边缘情况intval()与其他两个不同,但一般来说,它们会将字符串转换为 int。在没有 的情况下NaN0如果字符串是非数字且无法解析,则它们都输出(零)。但这对于大多数数据库卫生目的来说应该足够了。

If you really want a the NaNon error, the closest you'll get is null. You can get this using filter_var():

如果你真的想要一个NaNon 错误,你会得到的最接近的是null. 你可以使用filter_var()

$value = filter_var(FALSE, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);

If you just want to check that the string is numeric, the is_numeric()function can do that:

如果您只想检查字符串是否为数字,该is_numeric()函数可以这样做:

$bool = is_numeric($string);

However, it returns true for decimals and also for scientific notation, so may not be perfect for most situations. A better option is ctype_digit()which returns true if the string is made up only of digits:

但是,对于小数和科学记数法,它返回 true,因此在大多数情况下可能并不完美。更好的选择是ctype_digit(),如果字符串仅由数字组成,则返回 true:

$bool = ctype_digit($string);

If non e of this suits, there is always the regex option, using preg_match()or preg_replace(), but these are definitely overkill for this kind of scenario.

如果这些都不适合,那么总是有正则表达式选项,使用preg_match()or preg_replace(),但对于这种情况,这些绝对是矫枉过正。

回答by GBD

You can use regular expression as below

您可以使用正则表达式如下

$int = (int) preg_replace('/\D/', '', $strNonNumeric);

OR

或者

$int = (int) preg_replace('/[^0-9]/', '', $strNonNumeric);

回答by Wayne Whitty

You can use:

您可以使用:

$string = "90"; //string
$myInt = (int) $string; //parse to int

回答by Magnus Strand

I would recommend using FILTER_VALIDATE_INT Filter.

我建议使用 FILTER_VALIDATE_INT 过滤器。

about Filters on php.net

关于 php.net 上的过滤器

回答by enhzflep

You can also use sscanf (just like you do in C)

您也可以使用 sscanf(就像您在 C 中所做的那样)

<?php

    $str = "10";

    sscanf($str, "%d", $intVal);
    $intVal *= 10;

    echo $intVal;
?>

Output:

输出:

100

100