Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/
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
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75083
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?
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75098
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
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75110
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.
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75114
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]
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75177
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 ???
Link to comment
https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75180
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.