Skip to main content

Change Australian mobile numbers to +61 in PHP

Recently, I was using Laravel to build an online ordering system for a restaurant. The website required users to sign up and type in an SMS verification code to activate their account. Using Twilio, we wanting the phone number to come up as the business name instead of a phone number. When signing up though, Twilio required the phone number to be in international format (Eg. +6140122222) instead of 0401222222. So, we faced the dilemma of either using javascript to replace it, or just do it in PHP so that the user will not know any different.

It was really quite easy,

$phone = '0422222222';
$formatted = substr_replace($phone,"+61",0,2);

The phone number will be displayed like +614222222 in the $formatted variable.

Obviously this will work for other countries as well, but might be easier in Australia as we only have one type of mobile number starting with ’04’.

Hope that helps!

Leave a Reply