Recently, it became necessary to normalize phone numbers for my work on SimpleChurchCRM. In order to normalize something, we must first understand it and analyze it.
The following regular expression will match phone numbers and their parts.
([(]?(?P<area>\d{3})[)]?)?(?: ?-?)?(?P<three>\d{3})(?: ?-?)?(?P<four>\d{4})( (x|(ext\.? ))(?P<ext>\d+))?
Note that this requires PHP > 5.2.2 I think.
A phone number matched with preg_match will be broken into an array like the following:
Array
(
[0] => (123) 456-7890 x2
[1] => (123)
[area] => 123
[2] => 123
[three] => 456
[3] => 456
[four] => 7890
[4] => 7890
[5] => x2
[6] => x
[7] =>
[ext] => 2
[8] => 2
)
The only parts we care about are the values coorsponding to area, three, four, and ext. All we have to do it check to see which parts were matched and then assemble them with the formatting we desire. I prefer: 123-456-7890 x12.
This regex will match phone numbers with and without area codes and with and without extensions.
Here are some phone numbers that this matches:
$nums = array(
'1234567890',
'123 456 7890',
'4567890',
'456-7890',
'123-456-7890',
'(123) 456-7890',
'1234567890 ext. 1',
'123 456 7890 ext 12',
'4567890 ext. 4532',
'456-7890 x1235',
'123-456-7890 x54',
'(123) 456-7890 x2',
);
I hope this helps someone.
Brian

Comments
Post new comment