Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting a phone number 1

Status
Not open for further replies.

captlid

Technical User
Oct 27, 2004
82
US
$phone = '6467897654';
$phone = ereg_replace("([0-9]{3})([0-9]{3})([0-9]{4})", "(\1) \2-\3", $phone);

the output comes out like
() -

need phone number to look like (646)789-7654
And is there any difference between regex on windows and linux?
 
1. Start using PCRE instead of POSIX.
2. Adjust your pattern:
Code:
$phone = '6467897654';
$phone = preg_replace("/(\d{3})(\d{3})(\d{4})/", "(\\1) \\2-\\3", $phone);
echo($phone);
Note:
Escaping of backreferences is necessary when used in double quotes since the backslash has to be escaped in double quotes. Single quotes need no escaping:
Code:
$phone = '6467897654';
$phone = preg_replace("/(\d{3})(\d{3})(\d{4})/", '(\1) \2-\3', $phone);
echo($phone);
 
thanks that worked like a charm, I am guessing pcre has better cross compatibility?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top