Preg_match with PHP number

When I use this:

preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/', 0.0001);

      

The return value is 1 and when I use this:

preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/', 0.00001);

      

The return value is 0.

Can anyone tell me why please? Thank!

+3


source to share


1 answer


Since small floating point numbers are usually displayed using exponential notation, so is 0.00001

converted to 1.0E-5

, which does not match the regex. You can see this if you simply do:

echo 0.00001;

      



Regular expressions should be used with strings, not numbers.

preg_match('/^[0-9]{1,10}(\.[0-9]{1,9})?$/', '0.00001');

      

+5


source







All Articles