Jump to content


Photo

formatting postcode


  • Please log in to reply
11 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 12:44 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 August 2006 - 12:47 PM

http://regexlib.com/...x?regexp_id=260

http://www.qwghlm.co.uk/blog/?p=761
Legend has it that reading the manual never killed anyone.
My site

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 01:11 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 01:48 PM

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


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

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 15 August 2006 - 01:59 PM

Explain the rules of what you want to achieve. As Daniel mentioned--where is the logic?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 02:05 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 02:16 PM

<?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;
}
?>

This should do it if I understood it correctly.

#8 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 03:27 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 15 August 2006 - 03:44 PM

<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>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:52 PM

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


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

#11 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 04:08 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#12 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 August 2006 - 04:33 PM

Thanks effigy.. didnt see your post on my last post.. just tested it and working great! :)

SOLVED

Cheers for the help guys


Liam



www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users