Format a Telephone Number

Hi all, I have telephone numbers stored in my database, some have spaces, others not. How can I format telephone numbers to leave a space after the fifth character? Sure this is easy peezy but can’t work it out :confused: I want it to look like 01234 567890. Had a look on Google and all seem to be formatting in an American way!

function FormatPhone($phone)
        $phone = str_replace(" ", "", $phone);
        return substr($phone, 0, 5) . " " . substr($phone, 5);

echo FormatPhone("1234567890");

From your post it looks like they’re all UK phone numbers. Don’t forget that not all UK phone numbers have a 5-digit area code. See here for more info. Your script will need to use regex to determine what part of the number is the area code and what part is the main number.

Hey, thank you to the both of you for your help, much appreciated :slight_smile:

The UK telephone number formats seem especially complex but this spreadsheet provided all the clues we needed:

There are hundreds of code snippets listed around the web, but they seem to have many errors.

This one looks like it might be useful: