Alex Rivera | Logout

How do you format a 10 digit string into a phone number?

Asked 2010-01-18T23:16:36.667
21

I have database records in the form of 10 character long strings, such as 4085551234.

I wish to format these into this format: (408) 555-1234.

I think this is regex related. I'm new to programming and completely self-taught here, so any sort of resource relating to performing text processing would be appreciated as well. Thanks!

Edit
Report

1 Answer

6

I'd probably go with

$num = "4085551234";  // given                                                  
$formatted = "(".substr($num,0,3).") ".substr($num,3,3)."-".substr($num,6);

Regex isn't really appropriate here.

answered 2010-01-18T23:23:20.260

Your Answer