I have written a function that needs a little twekaing because it’s not working properly.
I want to pass it the first part of a UK postcode and if the first 2 digits are letters take one path and if the first digit is a letter and the second is a number take the other path.
The problem is that it’s not taking any path
function getPostcode($postcode) {
if(is_numeric($postcode{1})) {
$postcode = substr($postcode, 0, 1);
}
elseif(is_numeric($postcode{2})) {
$postcode = substr($postcode, 0, 2);
}
if(is_numeric($postcode{1})) {
$scotland = array('G');
$northwest = array('L');
$northeast = array('S');
$midlands = array('B');
$london = array('E', 'N', 'W');
}
elseif(is_numeric($postcode{2})) {
$scotland = array('AB', 'DD', 'DG', 'EH', 'FK', 'HS', 'IV', 'KA', 'KW', 'KY', 'ML', 'PA', 'PH', 'TD', 'ZE');
$ireland = array('BT');
$northwest = array('BB','BL','CA','CH','CW','FY','IM','LA','M','OL','PR','SK','SY','TF','WA','WN');
$northeast = array('BD', 'DH', 'DL', 'DN', 'HD', 'HG', 'HU', 'HX', 'LA', 'LS', 'NE', 'SR', 'TS', 'WF', 'YO');
$midlands = array('CV', 'DE', 'DY', 'LE', 'NN', 'NG', 'ST', 'WS', 'WV');
$anglia = array('AL', 'CB', 'CM', 'CO', 'EN', 'IG', 'IP', 'LU', 'MK', 'NR', 'PE', 'RM', 'SG', 'SS', 'WD');
$london = array('EC', 'NW', 'SE', 'SW', 'WC');
$southwest = array('BA', 'BH', 'BS', 'DT', 'EX', 'GL', 'HR', 'PL', 'TA', 'TQ', 'TR', 'WR');
$southcentral = array('GU', 'GY', 'HA', 'HP', 'JE', 'OX', 'PO', 'RG', 'SL', 'SN', 'SO', 'SP', 'UB');
$southeast = array('BN', 'BR', 'CT', 'CR', 'DA', 'KT', 'ME', 'RH', 'SM', 'TN', 'TW');
}
if(isset($scotland)) {
if(in_array($postcode, $scotland)) {
return "scotland";
}
}
if(isset($ireland)) {
if(in_array($postcode, $ireland)) {
return "northern-ireland";
}
}
if(isset($northwest)) {
if(in_array($postcode, $northwest)) {
return "north-west";
}
}
if(isset($northeast)) {
if(in_array($postcode, $northeast)) {
return "north-east";
}
}
if(isset($midlands)) {
if(in_array($postcode, $midlands)) {
return "midlands";
}
}
if(isset($anglia)) {
if(in_array($postcode, $anglia)) {
return "anglia";
}
}
if(isset($london)) {
if(in_array($postcode, $london)) {
return "london";
}
}
if(isset($southwest)) {
if(in_array($postcode, $southwest)) {
return "south-west";
}
}
if(isset($southcentral)) {
if(in_array($postcode, $southcentral)) {
return "south-central";
}
}
if(isset($southeast)) {
if(in_array($postcode, $southeast)) {
return "south-east";
}
}
else return "****ed up";
}