format phone number
Moderator: General Moderators
format phone number
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.
actually you wouldn't need to use regex for this... the simple string functions will do it....
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)
Code: Select all
$phone = 1234567890;
$newphone = substr($phone,0,3) . '-' . substr($phone,3,3) . '-' . substr($phone,6,4);