X, y, z are all numbers. Beside knowing how to check if the string is 14 chars long, I don't want to iterate each character and check if the char matches [0-9].
Are there smarter ways? No regex pls.... I'm scared of it.
X, y, z are all numbers. Beside knowing how to check if the string is 14 chars long, I don't want to iterate each character and check if the char matches [0-9].
Are there smarter ways? No regex pls.... I'm scared of it.
You can use sscanf
:
Example:
print_r( sscanf("+123-1234-1234", "+%3d-%4d-%4d") );
will output:
Array ( [0] => 123 [1] => 1234 [2] => 1234 )
This will not guarantee the validity of the input string though. A string like +1-1-1 would result in array having all three values with 1. So you still have to validate the three array values separately.
See the list of supported formats (not sure PHP supports all of these though).
But seriously, ^\+\d{3}-\d{4}-\d{4}$
is not something to be scared of. It's reasonably simple. There is much worse in Regex Land.