AdRock Posted March 27, 2010 Share Posted March 27, 2010 I have made a function that takes the first part of a postcode and assigns it to a region. The function works perfectly apart from if the postcode begins with 1 letter and then a number as my function is looking for 2 starting letters. When a postocde is entered such as E7 5RS the function does't find E7 so nothing is returned I was thinking about having a second part of the function so if the first 2 characters are letters then do these or do the others. How would i check the first part of the postcode to see where the first number occurs so if it occurs at position 2 it is a 2 letter postcode and if it occurs at postion 1 it's a single letter postcode function getPostcode($postcode) { $postcode = substr($postcode, 0, 2); $scotland = array('AB', 'DD', 'DG', 'EH', 'FK', 'G', 'HS', 'IV', 'KA', 'KW', 'KY', 'ML', 'PA', 'PH', 'TD', 'ZE'); $ireland = array('BT'); $northwest = array('BB','BL','CA','CH','CW','FY','IM','L','LA','M','OL','PR','SK','SY','TF','WA','WN'); $northeast = array('BD', 'DH', 'DL', 'DN', 'HD', 'HG', 'HU', 'HX', 'LA', 'LS', 'NE', 'S', 'SR', 'TS', 'WF', 'YO'); $midlands = array('B', '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('E', 'EC', 'N', 'NW', 'SE', 'SW', 'W', '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(in_array($postcode, $scotland)) { return "scotland"; } if(in_array($postcode, $ireland)) { return "northern-ireland"; } if(in_array($postcode, $northwest)) { return "north-west"; } if(in_array($postcode, $northeast)) { return "north-east"; } if(in_array($postcode, $midlands)) { return "midlands"; } if(in_array($postcode, $anglia)) { return "anglia"; } if(in_array($postcode, $london)) { return "london"; } if(in_array($postcode, $southwest)) { return "south-west"; } if(in_array($postcode, $southcentral)) { return "south-central"; } if(in_array($postcode, $southeast)) { return "south-east"; } } Link to comment https://forums.phpfreaks.com/topic/196691-help-needed-with-finding-the-first-occurence-of-a-number/ Share on other sites More sharing options...
Kieran Menor Posted March 27, 2010 Share Posted March 27, 2010 Perhaps is_numeric($postcode{0}), is_numeric($postcode{1}), etc. Link to comment https://forums.phpfreaks.com/topic/196691-help-needed-with-finding-the-first-occurence-of-a-number/#findComment-1032678 Share on other sites More sharing options...
AdRock Posted March 27, 2010 Author Share Posted March 27, 2010 Perhaps is_numeric($postcode{0}), is_numeric($postcode{1}), etc. Can you expand on that a bit more please? I'm not sure what you mean. Link to comment https://forums.phpfreaks.com/topic/196691-help-needed-with-finding-the-first-occurence-of-a-number/#findComment-1032679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.