phpregexstringereg-replace

How to remove initial (on the left) "+" or "0" from a telephone number string?


I need a regular expression in PHP to remove from a string of telephone numbers the + or the 0 at the beginning of a number.

I have this function to remove every not-number characters

ereg_replace("[^0-9]", "", $sPhoneNumber)

but I need something better, all these examples should be...

$sPhoneNumber = "+3999999999999"
$sPhoneNumber = "003999999999999"
$sPhoneNumber = "3999999999999"
$sPhoneNumber = "39 99999999999"
$sPhoneNumber = "+ 39 999 99999999"
$sPhoneNumber = "0039 99999999999"

... like this

$sPhoneNumber = "3999999999999"

any suggestions, thank you very much!


Solution

  • You can do this:

    $result = preg_replace('~^[0\D]++|\D++~', '', $sPhoneNumber);