public static function formatPhoneNumber($phone = '', $returnRaw = false, $trim = true, $convert = false)
{
/*
$returnRaw = will return the raw number after it cleans it. That's the number to save in db
$trim to: to cut it to a specific number
$convert: to change letters into numbers
*/
$phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);
if (empty($phone)) {
return '';
}
if ($returnRaw) {
return $phone;
}
//Convert Letters to #s
// Samples are: 1-800-TERMINIX, 1-800-FLOWERS, 1-800-Petmeds
if ($convert == true) {
$replace = array('2' => array('a', 'b', 'c'), '3' => array('d', 'e', 'f'), '4' => array('g', 'h', 'i'), '5' => array('j', 'k', 'l'), '6' => array('m', 'n', 'o'), '7' => array('p', 'q', 'r', 's'), '8' => array('t', 'u', 'v'), '9' => array('w', 'x', 'y', 'z'));
// Replace each letter with a number. str_ireplace instead of str_replace = case incensitive
foreach ($replace as $digit => $letters) {
$phone = str_ireplace($letters, $digit, $phone);
}
}
// If we have a number longer than 11 digits cut the string down to only 11
// This is also only ran if we want to limit only to 11 characters
if ($trim == true && strlen($phone) > 11) {
$phone = substr($phone, 0, 11);
}
// Perform phone number formatting here
if (strlen($phone) == 7) {
return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "\$1-\$2", $phone);
} else {
if (strlen($phone) == 10) {
return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "(\$1) \$2-\$3", $phone);
} else {
if (strlen($phone) == 11) {
return preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "\$1(\$2) \$3-\$4", $phone);
} else {
return $phone;
}
}
}
}