Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How do I break down a phone number and rebuild it?

by steveAZ98 (Monk)
on Nov 18, 2000 at 00:45 UTC ( #42281=note: print w/replies, xml ) Need Help??


in reply to How do I parse a telephone number?

I take a slightly different approach because I have to deal with foreign phone numbers also. This sub seems to work pretty good for me. If it can't find a format it just splits it up into groups of threes and sends it back.
HTH
sub fphone { shift; s/[\s|\n|\r]//g; # strip space and newlines s/[-|)|(|.]//g; # strip seperating chars s/(x|ex|ext\.)(.*)$//; # Extension my $ext = $2; # save extension my $n = ''; my $l = length($_); if (($l == 12)||($l == 13)) { m/(\+\d{2}|\d{3})(\d{3})(\d{3})(\d{3})/; $n .= "$1 $2 $3 $4"; } elsif (length == 11) { # us with leading 1 s/[+]//g; m/(\D)?(\d{3})(\d{3})(\d{4})/; $n .= "$1 " if $1 =~ /\d/; $n .= "($2) $3-$4"; } elsif ($l == 10) { # us regular s/[+]//g; m/(\d{3})(\d{3})(\d{4})/; $n .= "($1) $2-$3"; } else { s/(\d\d\d)/$1 /g; $n .= $_; } $n .= " Ext. $ext" if $ext; return $n; }

Replies are listed 'Best First'.
Re: Answer: How do I break down a phone number and rebuild it?
by Fastolfe (Vicar) on Nov 18, 2000 at 03:53 UTC
    I might have tried to generalize that even further, though you still have regional issues to worry about. I.e. this won't work for everyone:
    @FORMAT_PHONE = ( '%d%d%d', # 911 '%d-%d%d%d', # 1-411 '%d-%d%d%d%d', # 5-1212 ? '%d%d-%d%d%d%d', # 55-1212 ? '%d%d%d-%d%d%d%d', # 555-1212 '%d-%d%d%d-%d%d%d%d', # 1-222-2345 '%d%d%d-%d%d%d-%d%d%d', # 212-114-151 ? '(%d%d%d) %d%d%d-%d%d%d%d', # (800) 555-1212 '+%d (%d%d%d) %d%d%d-%d%d%d%d', # +1 (800) 555-1212 '+%d%d (%d%d%d) %d%d%d-%d%d%d%d', # +01 (800) 555-1212 '+%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # +011 (800) 555-1212 '+%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # +011 1 (800) 555-1212 '%d%d%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # 10321 (800) 555-1212 '%d%d%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # 10321 1 (800) 555-1212 '%d%d%d%d-%d%d%d (%d%d%d) %d%d%d-%d%d%d%d', # 1010-220 (800) 555-12 +12 '%d%d%d%d-%d%d%d %d (%d%d%d) %d%d%d-%d%d%d%d', # 1010-220 1 (800) 5 +55-1212 # etc... ); my %L2M; @L2M{qw: Q Z :} = 1; @L2M{qw: A B C :} = 2; @L2M{qw: D E F :} = 3; @L2M{qw: G H I :} = 4; @L2M{qw: J K L :} = 5; @L2M{qw: M N O :} = 6; @L2M{qw: P R S :} = 7; # some define this as PQRS @L2M{qw: T U V :} = 8; @L2M{qw: W X Y :} = 9; # some define this as WXYZ sub format_phone { my $number = shift; $number =~ s/[a-zA-Z]/$L2M{uc($1)}/eg; $number =~ s/\D//g; # maybe allow # and * later?? if (defined($FORMAT_PHONE[length($number)-3])) { return sprintf($FORMAT_PHONE[length($number)-3], split(//, $numb +er)); } return; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://42281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-05-29 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?