Jump to content

Archived

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

shocker-z

formatting postcode

Recommended Posts

Hi there i was just wondering if anyone has come up with a function or able to come up with a function that will change a postcode to the following format..

NG56QN will become NG5-6
N18HV will become N1-8
NE15ES will become NE1-5
M225EX will become M22-5

Is this possible? as there are a few formats that it can be..


Regex is not my strong side but i can only see this as being the answer :)

Regards
Liam

Share this post


Link to post
Share on other sites
http://regexlib.com/REDetails.aspx?regexp_id=260

http://www.qwghlm.co.uk/blog/?p=761

Share this post


Link to post
Share on other sites
so if i were to use the expression: ^(((([A-PR-UWYZ][0-9][0-9A-HJKS-UW]?)|([A-PR-UWYZ][A-HK-Y][0-9][0-9ABEHMNPRV-Y]?))\s{0,2}[0-9]([ABD-HJLNP-UW-Z]{2}))|(GIR\s{0,2}0AA))$

what function do i use to make it add the - inbetween? and how would i delete the last charactors after the number?

Sorry if it sounds a bit dumb but i havn't really worked with regex before..

Liam

Share this post


Link to post
Share on other sites
[quote author=shocker-z link=topic=104341.msg416125#msg416125 date=1155645845]
Hi there i was just wondering if anyone has come up with a function or able to come up with a function that will change a postcode to the following format..

NG56QN will become NG5-6
N18HV will become N1-8
NE15ES will become NE1-5
M225EX will become M22-5

Is this possible? as there are a few formats that it can be..


Regex is not my strong side but i can only see this as being the answer :)

Regards
Liam
[/quote]

I don't see any logic in how the - is placed, so how should PHP automaticly be able to "figure" it out?

Share this post


Link to post
Share on other sites
Explain the rules of what you want to achieve. As Daniel mentioned--where is the logic?

Share this post


Link to post
Share on other sites
I have a system that i paste addresses into i currently have a page where i convert to CAPs and limite the length of lines and change from comma seperated to seperate line of data but i past this into a syatem that needs 1 extra line in which is the postcode with any letters and 1st 1 or 2 numbers followed by the number before the next letter
e.g.

NG56QN is NG5 6QN and what i need is NG5-6 so all the first part linked with - to the 1st number before the letters at the end as these state diffrent specific demographic locations in the UK

Does that make it clearer? or just confusing? lol

Liam

Share this post


Link to post
Share on other sites
[code]<?php
$code = "NG56QN";

$length = strlen($code);
for($i=0; $i <= $length; $i++)
{
if(preg_match("/[0-9]{1,1}/",substr($code,$i,1)))
{
$split = $i+1;
break;
}
}

if(!empty($split))
{
echo substr($code,0,$split)."-".substr($code,$split,1);
}
else {
echo $code;
}
?>[/code]

This should do it if I understood it correctly.

Share this post


Link to post
Share on other sites
yeah that's exacly what i want the only problem is that some addresses can be e.g. NG245YW and your script returns NG2-4 instead of NG24-5

Any idea's on how to overcome this?

Thatnks for all your input :0

Regards
Liam

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

$tests = array(
'NG56QN',
'N18HV',
'NE15ES',
'M225EX',
);

foreach ($tests as $test) {
echo $test, ' => ';
preg_match('/^([A-Z]+)(\d+)(\d)(?!\d)/', $test, $matches);
echo $matches[1] . $matches[2] . '-' . $matches[3];
echo '<br /><br />';
}

?>
</pre>
[/code]

Share this post


Link to post
Share on other sites
[quote author=shocker-z link=topic=104341.msg416239#msg416239 date=1155655622]
yeah that's exacly what i want the only problem is that some addresses can be e.g. NG245YW and your script returns NG2-4 instead of NG24-5

Any idea's on how to overcome this?

Thatnks for all your input :0

Regards
Liam
[/quote]

Weren't the - supposed to be after the first number ???

Share this post


Link to post
Share on other sites
yeah sorry it is but if there are 2 numbers then it's after the second basicaly 99/100 times it is just before the last number in the string.. if that makes it easier kind of working with that insterad of after the 1st or 2nd?

Liam

Share this post


Link to post
Share on other sites
Thanks effigy.. didnt see your post on my last post.. just tested it and working great! :)

SOLVED

Cheers for the help guys


Liam


Share this post


Link to post
Share on other sites

×

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.