Format Phone Number


PHP

function a4_formatPhone($phone, $style = 1){
	$phone = preg_replace('/[^0-9]/','',$phone);

	if(substr($phone,0,1) == 1){
		$phone = substr($phone,1);
	}

	if (strlen($phone) == 7){
		sscanf($phone, "%3s%4s", $prefix, $exchange);
	}elseif(strlen($phone) == 10){
		sscanf($phone, "%3s%3s%4s", $area, $prefix, $exchange);
	}elseif(strlen($phone) > 10){
		sscanf($phone, "%3s%3s%4s%s", $area, $prefix, $exchange, $extension);
	}else{
		return $phone;
	}

	if($style == 1){
		$out = "";
		$out .= isset($area) ? $area . '-' : "";
		$out .= $prefix . '-' . $exchange;
		$out .= isset($extension) ? ' x' . $extension : "";
	}else{
		$out = "";
		$out .= isset($area) ? '(' . $area . ') ' : "";
		$out .= $prefix . '-' . $exchange;
		$out .= isset($extension) ? ' x' . $extension : "";
	}
	return $out;
}

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>