Format phone number in PHP

Users submit their phone number as part of a form. With PHP I would like to take an reasonable phone number format and convert it to:


I think of some exceptable number formats as:

(222) 333-4444
222 333-4444
1(222) 333-4444

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You can remove all string chars from the input and see what is left, then validate the result. In your examples, by removing any non numeric char you are left with either the phone with the area code (2223334444) or the same number with the country code (12223334444).

To remove all non numeric chars from a string you can use:
 $result = ereg_replace("[^0-9]", "", $string);

Best regards,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sypderAuthor Commented:
Thanks. That will work well. The one other thing I need to do is remove a leading 1, if it exists? I have not yet read my regexp book. That is going to be my reading on my flight tonight.
After using the above expression from Esopo
$result = ereg_replace("[^0-9]", "", $string);
$result = substr($result,-10); //This will trim the leading 1's
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sypderAuthor Commented:
Wow, that is obvious and simple. For some reason I was thinking complicated.
Are you aware that you didn't give me any points?
sypderAuthor Commented:
I am so sorry! I (wrongfully) assumed that the two posts were both by you. I know see they were not. How can I assign points after the fact?
You can request assistance at Community Support:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.