Heads up! This post was written 14 years ago. Some information might be outdated or may have changed since then.
function formatPhoneNumber($phone) {
return preg_replace("/([0-9]{4})([0-9]{3})([0-9]{2,})/", "$1 $2 $3", preg_replace("/[^0-9]/", "", $phone));
}