I am creating a callcenter application (it manages contact information queues for a group of distributed subscribers for communication), and the architecture indicates one field, spaces, dashes, etc. After quite a bit of analysis, I agree that this is best.
Based on the variability of the input for phone numbers (apostrophes, periods, dashes, and combinations of each), I built a simple function that deals with the user record, cutting off everything except the numbers themselves, as well as a "perestroika" that reformats the original number into something more attractive to the user.
Since they were useful to me, here is what I wrote so far:
public static function cleanPhoneNumbers($input) { return preg_replace("/[^0-9]/", "", $input); } public static function formatPhoneNumbers($phone_number) { if(strlen($phone_number) == 7) { return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone_number); } elseif(strlen($phone_number) == 10) { return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone_number); } else { return $phone_number; } }
Some caveats. My application is not available to international clients right now (there is a voip application built in that we don’t want to allow outside the US right now), so I did not find the time to configure for international opportunities. In addition, as this happens, I will most likely return to the refactor and support these functions later.
I have found one weakness so far that has been a little sick for me. In my application, I must prohibit calls that should be made according to the time zone depending on the time of day (for example, do not allow someone on the West Coast to call at 6:00 in the morning, when he is at 9:00 in the east). do this, I have to join a separate area code table to my table with phone numbers by comparing 3-digit area codes to get the time zone. But I can’t just compare the zip code with the phone number field because they will never match. So, I need to deal with extra SQL to get only the first three digits of the number. Not a game changer, but work and bewilderment nonetheless.