Return to Snippet

Revision: 5584
at March 17, 2008 15:11 by naz


Initial Code
function parseUkPostcode($postcode) {

	$postcode = strtoupper(ereg_replace("[^A-Za-z0-9]", "", trim($postcode)));

	$lenght = strlen($postcode);
	
	if($lenght < 5 OR $lenght == 4){
		
		$regex = '/^([A-Z]{1,2})([0-9][0-9A-Z]?)$/';
		
		if (preg_match($regex, $postcode, $part)) { 
			
			$code = array(
		                 'full' 	=> $part[1] . $part[2],
		                 'area'     => $part[1],
		                 'district' => $part[2],
		                 'outer'    => $part[1] . $part[2],
		                 'sector'   => NULL,
		                 'walk'     => NULL,
		                 'inner'    => NULL
		                 );		 
		
		}else{
			
			$code = NULL;
			
		}
		
	}else{
		
		$regex = '/^([A-Z]{1,2})([0-9][0-9A-Z]?)\s*([0-9])([A-Z]{2})$/';
		
		if (preg_match($regex, $postcode, $part)) { 
			
			$code = array(
		                 'full' => $part[1].$part[2].' '.$part[3].$part[4],
		                 'area'     => $part[1],
		                 'district' => $part[2],
		                 'outer'    => $part[1] . $part[2],
		                 'sector'   => $part[3],
		                 'walk'     => $part[4],
		                 'inner'    => $part[3] . $part[4]
		                 );		 
		
		}else{
			
			$code = NULL;
			
		}
	}
   return $code;                
}

Initial URL


Initial Description
This function can validate and parse UK postcode like SW1A 1AA in to formated array.

Initial Title
UK postcode parser

Initial Tags
regex, php, array

Initial Language
PHP