Re: Formatting phone numbers

From: Date: Sun, 23 Jun 2002 19:37:24 +0000
Subject: Re: Formatting phone numbers
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-103487@lists.php.net to get a copy of this message
preg_replace will do it. $returnValue = preg_replace("/(\d{3})(\d{3})(\d{4})/","($1)$2-$3",$returnValue); (\d{3})(\d{3})(\d{4}) = $0 or \\0 we aren't using this match we are using the subpatterns in brackets (\d{3}) =$1 or \\1 matches 3 numbers (\d{3}) =$2 or \\2 matches 3 numbers (\d{4}) =$3 or \\3 matches 4 numbers which we use to make the replacement ($1)$2-$3 http://za2.php.net/manual/en/function.preg-replace.php so that's the phone number, I'll leave the cc to you. Paul Roberts roberts_paul@bigfoot.com ++++++++++++++++++++++++ <?php function formatPhoneNum($prmValue) { $retrunValue=''; for($i=0; $i<strlen($prmValue); $i++) { if(ereg('[0-9]', substr($prmValue, $i,1))) $returnValue.=substr($prmValue,$i,1); } $returnValue = preg_replace("/(\d{3})(\d{3})(\d{4})/","($1)$2-$3",$returnValue); return $returnValue; } ?> <?php $phoneNum= '415123kjdf4567'; ?> <p>Phone Number: <?php echo formatPhoneNum($phoneNum); ?></p> </body> </html> ----- Original Message ----- From: "Corinne Shea" <corinne@idma.com> To: <php-general@lists.php.net> Sent: Sunday, June 23, 2002 8:06 PM Subject: [PHP] Formatting phone numbers Hi, I'd like to write a function that will take a string of characters and output it in a phone number format (xxx)xxx-xxxx and one for credit cards xxxx-xxxx-xxxx-xxxx. What I have below will currently remove non-numeric characters from the string, but I'm not sure how to do the rest of the formatting. Any help would be appreciated. By the way, any recommendations for good php tutorials for beginners? Thanks, Corinne <html> <head> <title></title> </head> <body> <? function formatPhoneNum($prmValue) { $retrunValue=''; for($i=0; $i<strlen($prmValue); $i++) { if(ereg('[0-9]', substr($prmValue, $i,1))) $returnValue.=substr($prmValue,$i,1); } return $returnValue; } ?> <? $phoneNum= '415123kjdf4567'; ?> <p>Phone Number: <?=formatPhoneNum($phoneNum)?></p> </body> </html> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#103487) next »