0

Example for a phone number in Malaysia starting with 01; if I add the country code, it will be +601 or 601.

Without country code: 0123456789; with country code: 60123456789 or +60123456789.

The result I want is +60123456789 but some users may enter like:

123456789
0123456789
60123456789

What is the method I need use to get my result like +60123456789?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
bot studio
  • 41
  • 1
  • 5

1 Answers1

2

You can use a regex pattern with preg_replace() to substitute those:

$phone = preg_replace('~^(?:0?1|601)~','+601', $phone);

See PHP demo at tio.run or regex pattern at regex101 (explanation on right side).

For further regex info see this nice Stackoverflow Regex FAQ.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46