I’m trying to validate a number by it’s length. This number has to have 4 digits so it passes the validation. The problem is when this number has 0’s to it’s left, like 0035.
Right now I’m at this:
echo (strlen ((string) 0025 ));
Which gives a total of 2, but I want this to count the 0’s to it’s left, so it gives me a total of 4.
Clearly the cast of the integer to string is not working, how can i do this?
You can’t do that way, a left zero means the number is octal and not decimal, you can use sprintf() to do that.
Example:
Live Test:
http://codepad.viper-7.com/VQr7Xz
Comment Answer:
Cláudio, numbers have infinite left zeros, although a user has explicitly type 2 or 3 left zeros there are more hidden left zeros, it’s a math basic, this is why it’s impossible to know how many left zeros the user has typed if you receive an
integervariable. If the variable has a constant size and you want to know how many left zeros it has you can do this:Live test: http://codepad.viper-7.com/fT2jSn
But if you the variable has variable length it must be a
stringtype instead of anumerictype.An example where the variable received is a
string:Live Test: http://codepad.viper-7.com/BTRTgR