If in fact users are entering the phone numbers you will want to do a much
better
job than this.
perople may or may not put the 1 in front of 800 etc., Letters are often
used for phone numbers, and there maybe be an extension indicated e.g.
1-800-mattress [sic -- there are often "extra" letters that don't matter]  
555-1234 ext 666 == 555-1234x666 etc. will would be reformatted to
555-123-4666

This is only for the U.S. -- international numbers make it even more
complex.

How is the database storing these numbers?  Just a string of characters?
Is there autodial or not?  If not considering leaving the string alone.
Otherwise you risk losing information.  A missing digit, especially in the
area
code or exchange portion can often be reconstructed by a person, e.g.
seeing a number such as 61 555-1234 I can guesss I have omitted a 7 when
typing.

Similarly people often enclose the area code in parens, or follow it with a
slash -- they almost
never use these characters after the exchange or before an extension.

Once it passes basic syntax validation 
you might want to use a hash to map letters to digits.

I would be interested in perl code that handled formatting phone numbers as
they
actually appear, e.g. in people's .signature lines etc.

Steve


> -----Original Message-----
> From: Uri Guttman [mailto:[EMAIL PROTECTED]]
> Sent: Friday, February 02, 2001 1:49 AM
> To: [EMAIL PROTECTED]
> Cc: [EMAIL PROTECTED]
> Subject: Re: [Boston.pm] unformatting phone numbers
> 
> 
> >>>>> "MJB" == Matthew J Brooks <[EMAIL PROTECTED]> writes:
> 
>   MJB> (555) 555-5555
>   MJB> 555-555-5555
>   MJB> 555.555.5555
>   MJB> 555 555-5555
>   MJB> ]
> 
>   MJB> I'd like to stip out the usual garbage that a user 
> would enter so
>   MJB> I can then reformat it. [Actually it'll be reformatted when it
>   MJB> comes out of the database... wouldn't want to waste 
> any precious
>   MJB> bytes on formatting :)]
> 
>   MJB> What I'd like to know is can anyone come up with 
> something less ugly than
>   MJB> this?
> 
>   MJB> $phone =~ s#(\(|\)|-|\.| )##g;
> 
> you need to learn about character classes. this is better and 
> faster as
> well:
> 
>       s/[(). -]//g ;
> 
> you can make that more efficient with:
> 
>       s/[(). -]+//g ;
> 
> or even faster with:
> 
>       tr/(). -//d ;
> 
> and it also depends on how strictly you want to filter out garbage.
> 
>       s/\D+//g ;
> 
> will strip out all non-digits.
> so will this and it is faster:
> 
>       tr/0-9//cd ;
> 
> uri
> 
> -- 
> Uri Guttman  ---------  [EMAIL PROTECTED]  ----------  
http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com

Reply via email to