检查变量是否为自然数
我想在网站上制作一个竞价系统。这意味着用户可以发布他们的出价(自然数)。我想确保用户不会尝试发布字符、十进制数字等。 我不想使用 is_numeric 函数,因为允许使用十六进制表示法。 我想为此使用 preg_match 。但在 php.net 中,这个函数的文档很少,我不知道如何使用 preg_match。
那么我应该如何使用 preg_match 检查变量是否是自然数呢?
I want to make a bid system on a website. That means users can post their bid (natural number). I want to make sure users don't try to post characters, decimal numbers, etc.
I don't want to use is_numeric function because hexadecimal notation is allowed.
I was thinking to use preg_match for this. But in php.net the documentation for this function is little and I have no idea how to use preg_match.
So how should I check if a variable is a natural number with preg_match?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果不需要小数点:
ctype_digit
或filter_var($var, FILTER_VALIDATE_INT)
。如果您这样做:
filter_var($var, FILTER_VALIDATE_FLOAT)
< /a>.If you don't require decimal points:
ctype_digit
orfilter_var($var, FILTER_VALIDATE_INT)
.If you do:
filter_var($var, FILTER_VALIDATE_FLOAT)
.ctype_digit 执行您想要的操作:
ctype_digit does what you want:
我知道这已经很老了,但我想分享下一个解决方案,以防其他人遇到这个问题。
我假设自然数指的是正整数(不包括数字 0)。
I know this is very old but I wanted to share the next solution in case someone else comes up with this problem.
I'm assuming that by natural number you meant positive integer (which excludes the number 0).
preg_match('/^[0-9]+$/', $var);
或ctype_digit
Either
preg_match('/^[0-9]+$/', $var);
orctype_digit
我通常会警告不要使用正则表达式来解析数字,因为通常有比正则表达式更好的解决方案,但既然你问了,我会尽力为你提供一些帮助:
preg_match
使用正则表达式(regex)用于它的匹配。您可以在 http://www.regular-expressions.info/
如果要匹配正则表达式中的数字,可以使用
[0-9]
或\d
。如果您想匹配一个或多个内容,则可以使用加号。
最后,正则表达式字符串需要包含在一对字符中。选择的字符通常是斜杠 (
/
) 字符,因为某些语言特别需要此字符,但 PHP 也允许使用其他字符;波形符 (~
) 很常见。因此,匹配任意位数的正则表达式字符串将是
"/\d+/"
。然后可以将其放入preg_match
调用中,如下所示:如果您有更具体的要求,您可以通过将加号替换为
{max}
来限制允许的字符数或{min,max}
,其中“min”和“max”是允许前一个匹配的次数。因此,要允许长度在两位到六位之间的数字,您可以使用以下内容:如果您需要允许小数点,您需要知道点字符是正则表达式中的特殊字符(这意味着“完全匹配任何字符” ),所以你需要用反斜杠转义它。
因此,匹配具有两位小数且小数点前至少一位数字的货币金额的正则表达式将如下所示:
最后,请注意,如果字符串简单地包含 匹配值。为了确保它不包含任何其他内容,您需要将其“锚定”到字符串的开头和结尾,使用锚定字符:
^
作为字符串的开头,>$
为结尾。因此,对于前面的示例,如果您希望它仅包含十进制数字,而没有其他内容,则需要以下内容:
正则表达式是一个复杂的主题,但我希望这能给您一个开始。
I would generally caution against using regex for parsing numerics, as there are generally better solutions than regex for this, but since you're asking, I'll try to give you some assistance with it:
preg_match
uses regular expressions (regex) for it's matching.You can find out more about regex syntax at sites like http://www.regular-expressions.info/
If you want to match a digit in regex, you can either use
[0-9]
or\d
.If you want to match one or more of anything, you would use a plus sign.
Finally, regex strings need to be enclosed in a pair of characters. The character chosen is usually a slash (
/
) character, as some languages specifically require this character, but PHP also allows other characters to be used; tilde (~
) is quite common.So your regex string to match any number of digits would be
"/\d+/"
. This can then be put into apreg_match
call like so:If you have more specific requirements, you can limit the number of characters allowed by replacing the plus sign with
{max}
or{min,max}
where 'min' and 'max' are the number of times the preceding match is allowed. So to allow a number between two and six digits long, you would use this:If you need to allow a decimal point, you need to know that the dot character is a special character in regex (it means 'match any character at all'), so you need to escape it with a back-slash.
Therefore, a regex to match a currency amount with two decimal places, and at least one digit before the point would be like this:
Finally, note that regex will return true in all the above if the string simply contains the matched value. To ensure it doesn't contain anything else, you would need to 'anchor' it to the front and end of the string, using the anchor characters:
^
for the start of the string, and$
for the end.So for the previous example, if you want it to only contain a decimal number, and nothing else, you would need this:
Regex is a complex subject, but I hope that gives you a start.
这取决于您对自然数的定义 - 根据不同的理论,数字零 (
0
) 算或不算自然数。要回答有关如何使用
preg_match
解决此问题的问题:如果您想包含零,使用 preg_match 非常简单
preg_match('^[0-9]+$', $input)
。用法:
也就是说 - 针对您的特定问题,使用
, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric valuectype_digit
是正如其他人已经指出的那样,更快的解决方案(如果您不想允许数字为零,则必须进行第二次检查)。如果您不想包含零,请使用
preg_match('^[1-9][0-9]*$', $input)
:也就是说 - 针对您的特定问题,使用
ctype_digit
是正如其他人已经指出的那样,更快的解决方案(如果您不想允许数字为零,则必须进行第二次检查)。This kind of depends on your definition of natural numbers - according to different theories, the number zero (
0
) does or does not count as a natural number.To answer your question on how to solve this with
preg_match
:If you want to include zero, using preg_match is pretty easy
preg_match('^[0-9]+$', $input)
.Usage:
That said - for your particular problem, using
, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric valuectype_digit
is a faster solution, as others already pointed out (you'd have to do a second check if you don't want to allow the number zero).If you don't want to include the zero, use
preg_match('^[1-9][0-9]*$', $input)
:That said - for your particular problem, using
ctype_digit
is a faster solution, as others already pointed out (you'd have to do a second check if you don't want to allow the number zero).简单的功能:
simple function:
从数学的角度来看,自然数是一个正整数,包括零,所以你可以这样检查:
is_int($bid) && $出价>=0
From a mathematical point of view, a natural number is a positive integer, including zero, so you could check it like this:
is_int($bid) && $bid >= 0
最简单、更快
Simplest and faster