Jump to content

[SOLVED] TRIMMING a value back to a space char and a letter char.


Voodoo Jai

Recommended Posts

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

 

 

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
)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.