I am storing U.S. phone numbers in MySql table as a bigInt(10).
Example: 1234567890
I want to convert that into a formatted phone number in either mysql or php
Example: 1234567890 => (123) 456-7890
Does anyone know how to accomplish this?
I am storing U.S. phone numbers in MySql table as a bigInt(10).
Example: 1234567890
I want to convert that into a formatted phone number in either mysql or php
Example: 1234567890 => (123) 456-7890
Does anyone know how to accomplish this?
<?php
function format_phone_number($number)
$number_string = '('.substr($number, 0, 3).') ';
$number_string .= substr($number, 3, 3).'-';
$number_string .= substr($number, 6);
return $number_string;
}
?>
Here, we use the substr
function three times to grab small text substrings of the whole number string. Remember that upon using this function, PHP will automatically turn the number into a string, so you don't have to worry about calling a string function on a variable that is the number type. It appends the extra characters (
, )
, and -
by using PHP's concatenation operator, the period. I split it up into three statements just for manageability, though you could perform this whole function in one long line if you'd like, as follows:
<?php
function format_phone_number($number)
return '('.substr($number, 0, 3).') '.substr($number, 3, 3).'-'.substr($number, 6);
}
?>