PHP is_numeric或preg_match 0-9验证 [英] PHP is_numeric or preg_match 0-9 validation

查看:240
本文介绍了PHP is_numeric或preg_match 0-9验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说不是一个大问题(据我所知),更多的是让我感兴趣的事情.但是,使用is_numeric而不是preg_match(反之亦然)来验证用户输入值的主要区别(如果有的话).

示例一:

<?php
    $id = $_GET['id'];
    if (!preg_match('/^[0-9]*$/', $id)) {
        // Error
    } else {
        // Continue
    }
?>

示例二:

<?php
    $id = $_GET['id'];
    if (!is_numeric($id)) {
        // Error
    } else {
        // Continue
    }
?>

我假设两者的功能完全相同,但是是否存在任何特定差异,这些差异可能会在以后以某种方式引起问题?有没有最佳方法"或我没有看到的东西使它们与众不同.

解决方案

is_numeric()测试值是否为数字.它不一定必须是整数-它可以是十进制数或科学计数法中的数字.

您给出的preg_match()示例仅检查值是否包含零到九的数字;数量不限,顺序不限.

请注意,您所提供的正则表达式也不是完美的整数检查器,就​​像您编写它的方式一样.它不允许有负面影响;它确实允许使用零长度的字符串(即完全没有数字,这大概应该是无效的吗?),并且它允许数字具有任意数量的前导零,而这又可能不是预期的.

根据您的评论,更好的正则表达式可能看起来像这样:

/^[1-9][0-9]*$/

这会强制第一个数字只能在1到9之间,因此您不能有前导零.它还强制其长度至少为一位数,从而解决了零长度字符串的问题.

您不必担心负面因素,所以这不是问题.

您可能希望限制位数,因为从目前的情况来看,它将允许太大的字符串存储为整数.为了限制这一点,您可以将星形更改为长度限制,如下所示:

/^[1-9][0-9]{0,15}$/

这将允许字符串在1到16位数字之间(即,第一位数字加上0-15位其他数字).随意调整花括号中的数字以适合您自己的需要.如果需要固定长度的字符串,则只需要在花括号中指定一个数字即可.

希望有帮助.

This isn't a big issue for me (as far as I'm aware), it's more of something that's interested me. But what is the main difference, if any, of using is_numeric over preg_match (or vice versa) to validate user input values.

Example One:

<?php
    $id = $_GET['id'];
    if (!preg_match('/^[0-9]*$/', $id)) {
        // Error
    } else {
        // Continue
    }
?>

Example Two:

<?php
    $id = $_GET['id'];
    if (!is_numeric($id)) {
        // Error
    } else {
        // Continue
    }
?>

I assume both do exactly the same but is there any specific differences which could cause problems later somehow? Is there a "best way" or something I'm not seeing which makes them different.

解决方案

is_numeric() tests whether a value is a number. It doesn't necessarily have to be an integer though - it could a decimal number or a number in scientific notation.

The preg_match() example you've given only checks that a value contains the digits zero to nine; any number of them, and in any sequence.

Note that the regular expression you've given also isn't a perfect integer checker, the way you've written it. It doesn't allow for negatives; it does allow for a zero-length string (ie with no digits at all, which presumably shouldn't be valid?), and it allows the number to have any number of leading zeros, which again may not be the intended.

[EDIT]

As per your comment, a better regular expression might look like this:

/^[1-9][0-9]*$/

This forces the first digit to only be between 1 and 9, so you can't have leading zeros. It also forces it to be at least one digit long, so solves the zero-length string issue.

You're not worried about negatives, so that's not an issue.

You might want to restrict the number of digits, because as things stand, it will allow strings that are too big to be stored as integers. To restrict this, you would change the star into a length restriction like so:

/^[1-9][0-9]{0,15}$/

This would allow the string to be between 1 and 16 digits long (ie the first digit plus 0-15 further digits). Feel free to adjust the numbers in the curly braces to suit your own needs. If you want a fixed length string, then you only need to specify one number in the braces.

Hope that helps.

这篇关于PHP is_numeric或preg_match 0-9验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆