PHP script to properly format phone numbers

Summary: One of the biggest challenges as a programmer lies in the fact that you have to think ahead for your users so that things work seamlessly.  On any site involving input fields that interact with a database, it is critical that the information be validated before going into the database, and with a bit of forethought you can make sure that the inputs are not only valid, but in a standardized format. Consider phone numbers as an example.  If you ask 10 people to write down their phone numbers, you might get several different responses:

  • 555.5555
  • 555-5555
  • 555.555.5555
  • 555-555-5555
  • (555)555.5555
  • (555)555-5555
  • 5555555
  • 5555555555
  • 1.555.555.5555
  • 1-555-555-5555
  • 1(555)555.5555
  • 1(555)555-5555
You get the idea.
Concept:  While you could write code that forces users to type in the phone number a certain way, you can also... Read more »