I have a string containing a ten-digit phone number and I want to format it with hyphens.
I am seeking a way to convert 123456790
to 123-456-7890
as phone numbers are typically formatted in the USA.
$areacode = substr($phone, 0, 3);
$prefix = substr($phone, 3, 3);
$number = substr($phone, 6, 4);
echo "$areacode-$prefix-$number";
You could also do it with regular expressions:
preg_match("/(\d{3})(\d{3})(\d{4})/",$phone,$matches);
echo "$matches[1]-$matches[2]-$matches[3]";
There are more ways, but either will work.