Problem with a function i've written

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";
	
}

Thats because this :

$postcode{1}

Will not do anything.
try,

$c2=substr($postcode,1,1);

if (is_numeric($c2){ #only one you need to test
$postcode=substr($postcode,0,1);
}else{
$postcode=substr($postcode,0,2);
}
if (strlen($postcode)==1){

scotland blah blah (single letter)

}else{

scotland blah blah (double letter)

}

Problem is that you’re replacing your $postcode string with a portion of itself, and then retesting that string portion to see if its numeric. The logic used to trim means that it won’t be a numeric, so you always return ‘***ed up’.

Try

function getPostcode($postcode) { 

    if(is_numeric($postcode{1})) { 
        $postcode = substr($postcode, 0, 1); 
        $scotland = array('G'); 
        $northwest = array('L'); 
        $northeast = array('S'); 
        $midlands = array('B'); 
        $london = array('E', 'N', 'W'); 
    } 
    elseif(is_numeric($postcode{2})) { 
        $postcode = substr($postcode, 0, 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"; 
     
} 

echo getPostcode('SW1W');

Should display “london”

Does that help?

It actually does work. php lets you access each character of a string using that syntax.
http://www.php.net/manual/en/language.types.string.php#language.types.string.substr

So it does…lol. for years I have been using square brackets $postcode[1] and that works too!