Page 1 of 1

format phone number

Posted: Thu Jun 27, 2002 12:35 pm
by calebsg
How can I format a string from a database like 9876543210 to appear as 987-654-3210? I am unfamiliar with regular expressions and not sure where to start.

Posted: Thu Jun 27, 2002 2:20 pm
by will
actually you wouldn't need to use regex for this... the simple string functions will do it....

Code: Select all

$phone = 1234567890;

$newphone = substr($phone,0,3) . '-' . substr($phone,3,3) . '-' . substr($phone,6,4);
you would need to check and make sure the number is ten digits though, or else it'd get a little messed up. something like (strlen($phone)==10)