/ Published in: PHP
This function can validate and parse UK postcode like SW1A 1AA in to formated array.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function parseUkPostcode($postcode) { if($lenght < 5 OR $lenght == 4){ $regex = '/^([A-Z]{1,2})([0-9][0-9A-Z]?)$/'; '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})$/'; '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; }