Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex using PHP, any non-zero number containing the digits 0-9

I have this regex:

/^[0-9]+$/i

Used in this code:

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

I want the following to be true: $var = 1; $var = 50; $var = 333;

And the following to be false: $var = 0; $var = 01; $var = 'abc';

I think what I have works so far except for the "0" part..?

I went through this guide (http://www.phpf1.com/tutorial/php-regular-expression.html) but was unable to come up with a complete answer.

Any help would be appreciated, thanks!

like image 625
Jay Avatar asked Nov 29 '22 17:11

Jay


1 Answers

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

Means: A non-zero digit followed by an arbitrary number of digits (including zero).

like image 94
NikiC Avatar answered Dec 05 '22 17:12

NikiC