SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 :/ I want it to look like 01234 567890. Had a look on Google and all seem to be formatting in an American way!

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function FormatPhone($phone)
    {
            
    $phone str_replace(" """$phone);
            return 
    substr($phone05) . " " substr($phone5);
    }

    echo 
    FormatPhone("1234567890"); 

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,016
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    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.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thank you to the both of you for your help, much appreciated

  5. #5
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The UK telephone number formats seem especially complex but this spreadsheet provided all the clues we needed:

    http://www.telecom-tariffs.co.uk/sabc.csv


    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:

    http://james.cridland.net/code/forma...nenumbers.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •