Clockwork SMS API
Menu
  • Home
  • API Docs
  • Integrations and plugins
  • Pricing
  • FAQs
  • Blog
  • Support
  • Log in
  • Sign up
Blog

Convert a mobile number into international format

Posted 20 Oct 2011, by John (Updated 12 May 2017)

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.

Take care: A UK message sent to a number with no international dialing code and no leading zero (starting with a 7) will be sent to Russia, as their dialling code is 7.

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:

  1. First get a list of international dialing codes. Here’s one you could use
  2. For each of your mobile numbers, remove any parentheses and the numbers they contain
  3. Strip any spaces or non-numeric characters
  4. Strip out any leading zeros
  5. 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.
  6. Check if the mobile number now starts with the appropriate dialing code – if it does you’re done
  7. 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.

Post navigation
Wordpress: Contact Form 7 SMS
New API Documentation

Recent

  • Use SMS alongside your email platform and CRM system
  • Integrate Clockwork SMS with your favourite platforms using Zapier
  • Connect Clockwork with your Shopify account using Zapier
  • Integrate text messaging with your CRM
  • Integrate Clockwork with Capsule CRM

Popular

  • Clockwork SlideShare: Our Easy Text Message API
  • Choosing SMS Software
  • Convert a mobile number into international format
  • Concatenated SMS
  • The GSM character set
Subscribe to updates

Ready to join thousands of other businesses?

Try Clockwork for free

Free test credit if you’re a business

  • API Docs
  • Integrations and plugins
  • Pricing
  • FAQs
  • Blog
  • Support
  • Legal
  • Site map
  • @ClockworkSMS
  • GitHub
Made in Manchester by Mediaburst

Online SMS


Textburst

SMS Surveys


SurveyMill

SMS API


Clockwork SMS API
Lucy Andy John

No matter who you are, or how many texts you send, we’re on hand to help

Get in touch
Crown Commercial Service Supplier Mediaburst's ISO 9001, ISO 14001 and IS0/IEC 27001 accreditations

© 2018 Mediaburst. All rights reserved. Mediaburst is a trading name of SRCL Limited, a company registered in England and Wales #03226910.