PHP is_numeric or preg_match 0-9 validation

前端 未结 11 1530
别跟我提以往
别跟我提以往 2020-12-13 02:48

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

相关标签:
11条回答
  • 2020-12-13 03:02

    is_numeric checks more:

    Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

    0 讨论(0)
  • 2020-12-13 03:02

    PHP's is_numeric function allows for floats as well as integers. At the same time, the is_int function is too strict if you want to validate form data (strings only). Therefore, you had usually best use regular expressions for this.

    Strictly speaking, integers are whole numbers positive and negative, and also including zero. Here is a regular expression for this:

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

    OR, if you want to allow leading zeros:

    /^[-]?[0]|[1-9][0-9]$/

    Note that this will allow for values such as -0000, which does not cause problems in PHP, however. (MySQL will also cast such values as 0.)

    You may also want to confine the length of your integer for considerations of 32/64-bit PHP platform features and/or database compatibility. For instance, to limit the length of your integer to 9 digits (excluding the optional - sign), you could use:

    /^0$|^[-]?[1-9][0-9]{0,8}$/

    0 讨论(0)
  • 2020-12-13 03:02

    Meanwhile, all the values above will only restrict the values to integer, so i use

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

    to allow float values too.

    0 讨论(0)
  • 2020-12-13 03:08

    You can use this code for number validation:

    if (!preg_match("/^[0-9]+$/i", $phone)) {
            $errorMSG = 'Invalid Number!';
            $error    = 1;
        }
    
    0 讨论(0)
  • 2020-12-13 03:10

    Not exactly the same.

    From the PHP docs of is_numeric:

    '42' is numeric
    '1337' is numeric
    '1e4' is numeric
    'not numeric' is NOT numeric
    'Array' is NOT numeric
    '9.1' is numeric
    

    With your regex you only check for 'basic' numeric values.

    Also is_numeric() should be faster.

    0 讨论(0)
  • 2020-12-13 03:13

    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.

    0 讨论(0)
提交回复
热议问题