?
Solved

Format phone number in PHP

Posted on 2006-04-06
8
Medium Priority
?
659 Views
Last Modified: 2012-06-27
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:

9998887777

I think of some exceptable number formats as:

1-222-333-4444
222-333-444
222333444
(222)333-4444
(222) 333-4444
222 333-4444
1(222) 333-4444
etc...

Thanks.
0
Comment
Question by:sypder
  • 3
  • 3
7 Comments
 
LVL 14

Accepted Solution

by:
Esopo earned 225 total points
ID: 16395471
Hi,

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,

Esopo.
0
 
LVL 3

Author Comment

by:sypder
ID: 16395893
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.
0
 
LVL 11

Assisted Solution

by:star_trek
star_trek earned 150 total points
ID: 16396145
After using the above expression from Esopo
$result = ereg_replace("[^0-9]", "", $string);
use
$result = substr($result,-10); //This will trim the leading 1's
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:sypder
ID: 16396156
Wow, that is obvious and simple. For some reason I was thinking complicated.
0
 
LVL 14

Expert Comment

by:Esopo
ID: 16396431
Are you aware that you didn't give me any points?
0
 
LVL 3

Author Comment

by:sypder
ID: 16396638
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?
0
 
LVL 14

Expert Comment

by:Esopo
ID: 16396664
You can request assistance at Community Support:
http://www.experts-exchange.com/Community_Support/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question