Preg_match for numbers in php

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.

Let's say I only allow this kind of format 2015-2016 which contains number and only one dash. How can I do this with preg_match? I tried the following, but with no luck.

$a = '2015-2016';
if(!preg_match('/[^0-9\-]/i',$a)) {
    then return not valid data`
} 

timss

9,7123 gold badges33 silver badges55 bronze badges

asked Jan 13, 2016 at 15:26

6

Hope this will help

preg_match('/^\d{4}-\d{4}$/', $string);

^ Start of the string

\d{4} match a digit [0-9] Exactly 4 times

- matches the character - literally

\d{4} match a digit [0-9] Exactly 4 times

$ End of the string

answered Jan 13, 2016 at 15:40

Preg_match for numbers in php

Neel IonNeel Ion

1,5471 gold badge11 silver badges14 bronze badges

$a = '2015-2016';
if(!preg_match('/^[0-9 \-]+$/',$a)) {
 then return not valid data } 

Try that.

Preg_match for numbers in php

worldofjr

3,8098 gold badges34 silver badges48 bronze badges

answered Jan 13, 2016 at 15:29

Preg_match for numbers in php

Chris GChris G

7856 silver badges20 bronze badges

2

By Alvin Alexander. Last updated: June 4, 2016

PHP integer FAQ: Can you demonstrate a regular expression pattern to match an integer?

Interestingly, while reading a book titled Advanced PHP Programming, the author mentions that the PHP is_int function doesn't work exactly the way you expect it to. It just tests to see whether a given variable is typed as an integer or something else, so if you give it a string like "123", it will return false. This is a bad thing when you're trying to test web form data, for example.

As a solution to this problem, the author suggests using a PHP regular expression to match whether the thing you're looking at is an integer, and he uses the following PHP preg_match integer regex pattern to test the match:

if (preg_match('/^\d+$/', $var) 
{
  # do your stuff here ...
}

The PHP preg_match integer regular expression used in this example can be broken down like this:

  • The '^' refers to the beginning of the string.
  • The '\d+' means "one or more numeric digits".
  • The '$' means the end of the line (or end of the string).
  • The $var is the name of the variable you want to test.

Put all this together, and it means "Test the variable $var to see if it contains one or more numeric digits; because the beginning of line and end of line are specified, that's all that can be in this string, digits. For instance, the string '123' is valid, but a string like 'a123' is not valid."

I hope this PHP preg_match integer regular expression pattern (regex pattern) example has been helpful. There's not much to it, but if you're new to regular expressions, it can be helpful to see something like this broken down. It can also be a nice reminder for those of us with bad memories, lol.

How do I allow only numbers in PHP?

In PHP, for a "only numbers" validation you can use different approaches:.
is_int or is_integer..
is_numeric..
regular expressions..
ctype_digit..
filter_var..

What is the use of Preg_match in PHP?

The preg_match() function returns whether a match was found in a string.

Is numeric validation in PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How can I set 10 digit mobile number in PHP?

php $value = '9987199871'; $mobileregex = "/^[1-9][0-9]{10}$/" ; echo "Hi " .