Phone number beutifier
This function clean up phone number and present it in styled way.
Copy this code and paste it in your HTML
function format_phone($phone)
{
$prefix = null;
//Check if number has international prefix
switch(true){
case substr($phone, 0, 1) == '+': $prefix = '+';
break;
case substr($phone, 0, 2) == '00': $prefix = '+';
break;
}
//Strip all non numeric characters
case 6:
return preg_replace("/([0-9]{2})([0-9]{2})([0-9]{2})/", "$1-$2-$3", $phone); break;
case 7:
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); break;
case 10:
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone); break;
case 11:
return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break;
case 12:
return preg_replace("/([0-9]{4})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break;
default:
return $phone;
break;
}
}
Report this snippet
Comments
Subscribe to comments