Convert a mobile number into international format
When sending through Clockwork, Textburst or SurveyMill the delivery platform relies on your mobile phone numbers having the correct international format.
People format mobile numbers in all sorts of ways
Ask a room full of people to write down their mobile number and you’ll get many different formats, like:
07712345678
0771 234 5678
07712 345678
447712345678
+447712345678
44(0)7712345678
Now, add in a load of dialling codes for different countries and things get really messy.
Formatting your mobile numbers consistently before you upload them helps our apps know for sure to which country they should deliver your messages.
Adding international dialling codes manually
If you send worldwide, you’ll always have to convert your mobile numbers to international format by prefixing with the appropriate international dialling code. For the UK the dialling code is 44, so you’ll need to add this and remove the leading zero.
For example 07712345678
would become 447712345678
.
You can re-format numbers easily with Excel, quickly adding international codes, removing spaces and special characters – and many of our users already do it that way.
Using a script to convert mobile numbers to international format
If you’re asking users to enter their number in a form, or you’re using one of our plugins, the responsibility of submitting numbers in the right format is now in your customer, the purchaser or the ‘receivers’ hands, depending on which plugin you’re using.
Before you can send messages back to them, you’ll need to convert their mobile numbers into the valid international format for their country. That way our apps will receive consistently formatted data and our SMS API can route the messages to the correct country.
With a simple script that follows some easy steps, you can ensure all messages go where they should. Use it alongside one of our plugins, or within your own app:
- First get a list of international dialing codes. Here’s one you could use
- For each of your mobile numbers, remove any parentheses and the numbers they contain
- Strip any spaces or non-numeric characters
- Strip out any leading zeros
- For each number, take the country of residence of the person receiving the message and look it up on the table of dialling codes. If you don’t know the recipient’s country you could use your default country.
- Check if the mobile number now starts with the appropriate dialing code – if it does you’re done
- If not then add the dialling code to the beginning of the number and you’re good to go.
PHP example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
//The mobile numbers you need to internationalise:
$numbers = array(
'07123 456 781' => 'US',
'07123456782' => 'UK',
'07123456783' => '',
'447123456784' => 'UK',
'+44(0)7123456785' => 'UK',
'17123456786' => 'US'
);
//An array of country codes:
//Get a full list at: https://plugins.svn.wordpress.org/mediaburst-ecommerce-sms-notifications/tags/1.2.1/country-calling-codes.php
$numbers = array(
'UK' => '44',
'US' => '1'
);
//The default country code if the recipient's is unknown:
$default_country_code = '44';
//Loop through the numbers and make them international format:
foreach ( $numbers as $n => $c )
{
//Remove any parentheses and the numbers they contain:
$n = preg_replace("/\([0-9]+?\)/", "", $n);
//Strip spaces and non-numeric characters:
$n = preg_replace("/[^0-9]/", "", $n);
//Strip out leading zeros:
$n = ltrim($n, '0');
//Look up the country dialling code for this number:
if ( array_key_exists($c, $country_codes) ) {
$pfx = $country_codes[$c];
} else {
$pfx = $default_country_code;
}
//Check if the number doesn't already start with the correct dialling code:
if ( !preg_match('/^'.$pfx.'/', $n) ) {
$n = $pfx.$n;
}
//return the converted number:
echo $n."\r\n";
//Outputs: 17123456781 447123456782 447123456783 447123456784 447123456785 17123456786
}
?>
More tools and help
If you need to send SMS text messages with your re-formatted international numbers, it’s worth looking at our popular SMS apps Clockwork, Textburst and SurveyMill.
Hope all this helps but as always, if you have any questions please get in touch.
And if you haven’t tried Clockwork yet, sign up for free.