SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string to phone number

    I have a string

    8005551212

    and am trying to get it into this format

    (800) 555-1212

    is there a quick and easy way to do this or do i need to break the string down, and use some sort of a for loop to add the ()- to the number?

    thanks

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I think that the following regular expression should do it:

    PHP Code:
    $number preg_replace('/(\d{3})(\d{3})(\d{4})/''($1) $2-$3'$number); 
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use the preg_replace function and do something like this using regular expressions

    PHP Code:
    $formattedNumber preg_replace("/([0-9]{1,3})([0-9]{1,3})([0-9]{1,4})","(\1) \2-\3",$phoneNumber); 
    it should work but I didn't test it


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
  •