CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
formatting a phone number

 



chomdang
Deleted

Aug 9, 2000, 6:48 AM

Post #1 of 5 (453 views)
formatting a phone number Can't Post

Hi everyone..,
I would like to know if there is a module that can format a string (for example, a phone number).
If I have for example:
4443333456 => (444) 333-3456
444 333 3456 => the same
444-333-3456 => the same

Thanks in advance
Dang


Kanji
User / Moderator

Aug 8, 2000, 10:04 PM

Post #2 of 5 (453 views)
Re: formatting a phone number [In reply to] Can't Post

I don't know of any modules that format a string (although you should go take a look at the printf and sprintf perl functions), but you can achieve what you want with a regexp...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

sub phone_num_fmt {
local $_ = shift;

if ( /(\d{3})\D*(\d{3})\D*(\d{4})/ ) {
return "($1) $2-$3";
} else {
warn "Couldn't extract phone number from $_";
return $_;
}
}

my $no = phone_num_fmt( "4443333456" );</pre><HR></BLOCKQUOTE>


Cure
User

Aug 8, 2000, 10:10 PM

Post #3 of 5 (453 views)
Re: formatting a phone number [In reply to] Can't Post

perldoc perlre


Cure


Cameron
Deleted

Aug 8, 2000, 10:32 PM

Post #4 of 5 (453 views)
Re: formatting a phone number [In reply to] Can't Post

I didn't see any modules,
but this can be accomplished by a number of different ways.
here is a substitution that may work for you
s/.*?(\d{3}).?(\d{3}).?(\d{4})/\(\1\)\ \2-\3/;


Cameron


chomdang
Deleted

Aug 9, 2000, 12:57 PM

Post #5 of 5 (453 views)
Re: formatting a phone number [In reply to] Can't Post

Hi everyone...,
I found on cpan a module (number::format). I found in this module "format_picture($number, $picture);". The module do a lot of other thing (ex.: 111045$ will become
$111 045.00.

if someone wanna try it...download the module
and try this little script:
#...
print "Content-type: text/html \n\n";
use Number::Format;
$pho = new Number::Format();
$phone = $pho->format_picture(4445556677, '(###) ###-####');
print"$phone";

the result is (444) 555-6677
regards
dang


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives