[clug-progsig] RegEx
Shawn
sgrover at open2space.com
Wed Aug 12 02:31:02 PDT 2009
I'm working on a php migration routine where I need to take a list of
phone numbers, which may or may not be properly entered, then format
them into a a common format.
To do this I have found a routine
(http://benramsey.com/archives/making-it-valid-telephone-numbers/) that
use a regular expression to find the elements of the number. The
regular expression is:
$pattern =
'/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
In my case, if the number doesn't fit the usual patterns, I just return
the original value. (cleaning up the data while migrating, where possible)
This works well thus far, unless the number starts with the country code
(i.e. "1-403-111-2222"). In these cases I end up with a number
formatted as "(140) 311-1222 x.2". So I need to modify the regex to
handle the country code.
I've tried this:
$pattern =
'/^(1?)[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
(assuming I only ever see the country code of "1") But this doesn't
work well.
Anyone more familiar with regular expressions have any suggestions?
Thanks bunches. I'll happily post my full format function once I get it
working properly, should anyone need it...
Shawn
More information about the clug-progsig
mailing list