php从字符串的开头删除所有0 - 可能是未知数量[重复]

This question already has an answer here:

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?

</div>

Use ltrim()

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

Working DEMO

Use ltrim function.

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

DEMO.

also you can use regexp

$result = preg_replace('/^0*/', '', $number);

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');