PHP is_numeric or preg_match 0-9 validation

前端 未结 11 1586
别跟我提以往
别跟我提以往 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: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.

提交回复
热议问题