Voodoo Jai Posted August 23, 2008 Share Posted August 23, 2008 I have a postcode field that I want to separate into two parts like postcode value "HU5 3UD" to give "HU5" and "HU" I am wanting to strip the end chars of the original value, but the postcode comes in a few different formats: "H4 2ER" or "HU5 3UD" "HU12 23DE" you get the picture. all I want is the first "letter" chars and then the first "letter chars plus the first "number" chars. How could I do this. Many thanks in advance all VoodooJai Link to comment https://forums.phpfreaks.com/topic/120995-solved-trimming-a-value-back-to-a-space-char-and-a-letter-char/ Share on other sites More sharing options...
Barand Posted August 23, 2008 Share Posted August 23, 2008 If you are meaning UK postcodes, the second part is always in the form "9XX". The first part is X or XX folowed by 9 or 99 try function parse_pcode ($pc) { $res = array(); $k = strlen($pc); $a = trim(substr($pc, 0, $k-3)); $b = substr($pc, -3); $j = 0; $k = strlen($a); $res[0] = is_numeric($a[1]) ? $a[0] : $a[0].$a[1]; $res[1] = substr($a, -($k - strlen($res[0]))); $res[2] = $b[0]; $res[3] = $b[1].$b[2]; return $res; } $ar = parse_pcode('EC121AA'); echo '<pre>', print_r($ar, true), '</pre>'; ?> --> Array ( [0] => EC [1] => 12 [2] => 1 [3] => AA ) Link to comment https://forums.phpfreaks.com/topic/120995-solved-trimming-a-value-back-to-a-space-char-and-a-letter-char/#findComment-623909 Share on other sites More sharing options...
Voodoo Jai Posted August 24, 2008 Author Share Posted August 24, 2008 That looks great many thanks for the solution, I shall use this to great effect. Sometimes its hard to know how to go about solving a problem but when the solution is seen it all comes clear. Thanks again VoodooJai Link to comment https://forums.phpfreaks.com/topic/120995-solved-trimming-a-value-back-to-a-space-char-and-a-letter-char/#findComment-624174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.