检查变量是否为自然数

发布于 2024-11-18 01:00:30 字数 205 浏览 6 评论 0原文

我想在网站上制作一个竞价系统。这意味着用户可以发布他们的出价(自然数)。我想确保用户不会尝试发布字符、十进制数字等。 我不想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

厌味 2024-11-25 01:00:30

ctype_digit 执行您想要的操作:

检查提供的字符串、文本中的所有字符是否都是数字。

(在 PHP 5.1.0 之前,当文本为空字符串时,此函数返回 TRUE。)

ctype_digit does what you want:

Checks if all of the characters in the provided string, text, are numerical.

(Before PHP 5.1.0, this function returned TRUE when text was an empty string.)

酷遇一生 2024-11-25 01:00:30

我知道这已经很老了,但我想分享下一个解决方案,以防其他人遇到这个问题。

我假设自然数指的是正整数(不包括数字 0)。

function is_positive_integer( $value ) {

    // Check if is integer and greater than zero
    if( is_int( $value ) && $value > 0 ) {
        return true;
    }

    // Is not a positive integer
    else {
        return false;
    }
}

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).

function is_positive_integer( $value ) {

    // Check if is integer and greater than zero
    if( is_int( $value ) && $value > 0 ) {
        return true;
    }

    // Is not a positive integer
    else {
        return false;
    }
}
恬淡成诗 2024-11-25 01:00:30

preg_match('/^[0-9]+$/', $var);ctype_digit

Either preg_match('/^[0-9]+$/', $var); or ctype_digit

煮茶煮酒煮时光 2024-11-25 01:00:30

我通常会警告不要使用正则表达式来解析数字,因为通常有比正则表达式更好的解决方案,但既然你问了,我会尽力为你提供一些帮助:

preg_match 使用正则表达式(regex)用于它的匹配。

您可以在 http://www.regular-expressions.info/

如果要匹配正则表达式中的数字,可以使用 [0-9]\d

如果您想匹配一个或多个内容,则可以使用加号。

最后,正则表达式字符串需要包含在一对字符中。选择的字符通常是斜杠 (/) 字符,因为某些语言特别需要此字符,但 PHP 也允许使用其他字符;波形符 (~) 很常见。

因此,匹配任意位数的正则表达式字符串将是 "/\d+/"。然后可以将其放入 preg_match 调用中,如下所示:

$isnumeric = preg_match("/\d+/",$input_string);

如果您有更具体的要求,您可以通过将加号替换为 {max} 来限制允许的字符数或{min,max},其中“min”和“max”是允许前一个匹配的次数。因此,要允许长度在两位到六位之间的数字,您可以使用以下内容:

$isnumeric = preg_match("/\d{2,6}/",$input_string);

如果您需要允许小数点,您需要知道点字符是正则表达式中的特殊字符(这意味着“完全匹配任何字符” ),所以你需要用反斜杠转义它。

因此,匹配具有两位小数且小数点前至少一位数字的货币金额的正则表达式将如下所示:

$isnumeric = preg_match("/\d+\.\d\d/",$input_string);

最后,请注意,如果字符串简单地包含 匹配值。为了确保它不包含任何其他内容,您需要将其“锚定”到字符串的开头和结尾,使用锚定字符:^作为字符串的开头,>$ 为结尾。

因此,对于前面的示例,如果您希望它包含十进制数字,而没有其他内容,则需要以下内容:

$isnumeric = preg_match("/^\d+\.\d\d$/",$input_string);

正则表达式是一个复杂的主题,但我希望这能给您一个开始。

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 a preg_match call like so:

$isnumeric = preg_match("/\d+/",$input_string);

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:

$isnumeric = preg_match("/\d{2,6}/",$input_string);

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:

$isnumeric = preg_match("/\d+\.\d\d/",$input_string);

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:

$isnumeric = preg_match("/^\d+\.\d\d$/",$input_string);

Regex is a complex subject, but I hope that gives you a start.

你又不是我 2024-11-25 01:00:30

这取决于您对自然数的定义 - 根据不同的理论,数字零 (0) 算或不算自然数。

要回答有关如何使用 preg_match 解决此问题的问题:

如果您想包含零,使用 preg_match 非常简单 preg_match('^[0-9]+$', $input)
用法:

if (preg_match('^[0-9]+

如果您不想包含零,请使用 preg_match('^[1-9][0-9]*$', $input)

if (preg_match('^[1-9][0-9]*

也就是说 - 针对您的特定问题,使用 ctype_digit 是正如其他人已经指出的那样,更快的解决方案(如果您不想允许数字为零,则必须进行第二次检查)。

, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric value

如果您不想包含零,请使用 preg_match('^[1-9][0-9]*$', $input)



也就是说 - 针对您的特定问题,使用 ctype_digit 是正如其他人已经指出的那样,更快的解决方案(如果您不想允许数字为零,则必须进行第二次检查)。

, $input)) // $input represents a positive numeric value else // $input does not represent a positive numeric value

也就是说 - 针对您的特定问题,使用 ctype_digit 是正如其他人已经指出的那样,更快的解决方案(如果您不想允许数字为零,则必须进行第二次检查)。

, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric value

如果您不想包含零,请使用 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:

if (preg_match('^[0-9]+

If you don't want to include the zero, use preg_match('^[1-9][0-9]*$', $input):

if (preg_match('^[1-9][0-9]*

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).

, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric value

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).

, $input)) // $input represents a positive numeric value else // $input does not represent a positive numeric value

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).

, $input)) // $input represents a non-negative numeric value else // $input does not represent a non-negative numeric value

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).

情徒 2024-11-25 01:00:30
in_array(str_replace(str_split('0123456789'), '', $s), array(',','.',''));
in_array(str_replace(str_split('0123456789'), '', $s), array(',','.',''));
旧伤还要旧人安 2024-11-25 01:00:30

简单的功能:

function isnature($x){
$y = ceil($x)-floor($x);
return $y == 0 ? true : false;
}

simple function:

function isnature($x){
$y = ceil($x)-floor($x);
return $y == 0 ? true : false;
}
删除→记忆 2024-11-25 01:00:30

从数学的角度来看,自然数是一个正整数,包括零,所以你可以这样检查:

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

我们的影子 2024-11-25 01:00:30

最简单、更快

                if( is_numeric( $key ) &&  intval( $key ) == $key )
                 {
                      //key == number
                 }

Simplest and faster

                if( is_numeric( $key ) &&  intval( $key ) == $key )
                 {
                      //key == number
                 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文