romain
romain

Reputation: 33

preg_match with number PHP

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.

Somebody could say me why please ? Thanks !

Upvotes: 3

Views: 95

Answers (1)

Barmar
Barmar

Reputation: 780782

Because small floating point numbers are normally displayed using exponential notation, so 0.00001 is converted to 1.0E-5, which doesn't match the regexp. 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');

Upvotes: 5

Related Questions