-3

I have a telephone verification system in place. This allows the customer to enter a phone number. However, how can I remove all 0's from the start of the string?

For example I could use substr to remove one or two 0's but what if the customer enters ten or twelve of them?

i.e The customer could enter one of the following numbers:

  • 077223704750
  • 0077223704750
  • 00000077223704750
  • 0000000000077223704750`

How can I remove all 0's from the start only, not from the rest of the string?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
user2183216
  • 359
  • 3
  • 9
  • 22
  • what have you tried so far please .. explain what is not working and were you got stuck – NullPoiиteя May 09 '13 at 10:56
  • why not limit range ....why are you taking value more then limit and why not validation ? – NullPoiиteя May 09 '13 at 10:59
  • if it's a telephone number and the user enters ten zeros at the front, then it's not a valid phone number, so you should reject it rather than trying to fix it. – Spudley May 09 '13 at 11:56
  • If you want to validate UK phone numbers (you hinted they're UK in one of the comments), you might be interested in this answer: http://stackoverflow.com/questions/32401/validate-a-uk-phone-number/7728183#7728183 – Spudley May 09 '13 at 11:58

4 Answers4

15

Use ltrim()

$string = ltrim($string, '0');

Working DEMO

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
8

Really? Leading 0s are dialling prefixes -- if you take them off, the phone number is not the same & may (likely) be undiallable. You make also encounter plus '+' which is an international dialling prefix, and is (meant to be) standard from location to location.

If you really want to remove leading '0's, then use ltrim.

$trimmed = ltrim( $phone, '0');
Thomas W
  • 13,940
  • 4
  • 58
  • 76
  • Also note, that leading `+` in the international phone numbers has an official alias that is `00` – pozs May 09 '13 at 11:01
  • I'll be handling all the prefixes, in a seperate part of the code, just want to remove the 0 as this will likely need to be replaced with +44 – user2183216 May 09 '13 at 11:04
  • 1
    Really, if the user enters too many zeros, and you KNOW all the prefixes, just give him an error because "0-00.." or "00-00..." is no valid phone number. Do not try to remove something that is an error, do not try to repair it without the users consent and feedback. – Sven May 09 '13 at 12:44
  • You're assuming far too much. Unless you **really really** know what you're doing, don't bungle the data -- correct values can never be recovered. Only amateurs do irreversible data-loss like this. – Thomas W May 10 '13 at 01:49
4

Use ltrim function.

$number=  ltrim($number,'0');

DEMO.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • That won't trim the `0`s off the beginning of the string - you need to assign the result of `ltrim()` back to `$number` – Bojangles May 09 '13 at 10:57
4

also you can use regexp

$result = preg_replace('/^0*/', '', $number);
tanaydin
  • 5,171
  • 28
  • 45