tags:

views:

320

answers:

3

So far I have simple "numbers" only ...

/^[0-9]+$/

How can it be done to not allow leading zero (not start with zero) or preg_replace that would remove all spaces and leading zero ? Thank You

+5  A: 
/^[1-9][0-9]+$/
Eimantas
Just a quick note to say that these should all use `*`, not `+`, or they will incorrectly reject single digit numbers.If you are really matching integers, you may want to add `0|-?` at the beginning to match zero and negative numbers.
Mark
+1  A: 
/^[^0][0-9]+$/
inkedmn
this one is incorrect. [^0] will accept anything except 0
w35l3y
+2  A: 

Only numbers not starting with 0:

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

Remove all leading spaces and zero:

$num = preg_replace('/^(?:0|\s)*([0-9]+)$/', '\1', ' 0999');

To remove all spaces in string, also those not leading, use str_replace. It can be done with regex, but if you are going to loop many numbers that would be slower.

OIS
Hi AllThank you for your answers.I need this for the validation in forms ...as all mobile phones starts with 0 (as far as i know even here in Sweden)I need a dropdown list will ready country call codes and later combine mobile number there should not be a zero ...example 46 700700555 in order to send SMS verification code ...now is the problem where to find ready data with countries and their call codes.Thank you to all of you, this was very helpfull.
Feha
In that case, you probably don't want to use a full matching regex (with `^$`). You probably want to strip out all non-numeric symbols and any leading 0. Something like:`preg_replace('/[^0-9]//', '', $num);``preg_replace('/^0+//', '', $num);`
Mark