shocker-z Posted August 15, 2006 Share Posted August 15, 2006 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-6N18HV will become N1-8NE15ES will become NE1-5M225EX will become M22-5Is 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 :)RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/ Share on other sites More sharing options...
AndyB Posted August 15, 2006 Share Posted August 15, 2006 http://regexlib.com/REDetails.aspx?regexp_id=260http://www.qwghlm.co.uk/blog/?p=761 Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75071 Share on other sites More sharing options...
shocker-z Posted August 15, 2006 Author Share Posted August 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75083 Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 [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-6N18HV will become N1-8NE15ES will become NE1-5M225EX will become M22-5Is 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 :)RegardsLiam[/quote]I don't see any logic in how the - is placed, so how should PHP automaticly be able to "figure" it out? Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75098 Share on other sites More sharing options...
effigy Posted August 15, 2006 Share Posted August 15, 2006 Explain the rules of what you want to achieve. As Daniel mentioned--where is the logic? Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75107 Share on other sites More sharing options...
shocker-z Posted August 15, 2006 Author Share Posted August 15, 2006 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 lettere.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 UKDoes that make it clearer? or just confusing? lolLiam Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75110 Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75114 Share on other sites More sharing options...
shocker-z Posted August 15, 2006 Author Share Posted August 15, 2006 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-5Any idea's on how to overcome this?Thatnks for all your input :0RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75170 Share on other sites More sharing options...
effigy Posted August 15, 2006 Share Posted August 15, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75177 Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 [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-5Any idea's on how to overcome this?Thatnks for all your input :0RegardsLiam[/quote]Weren't the - supposed to be after the first number ??? Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75180 Share on other sites More sharing options...
shocker-z Posted August 15, 2006 Author Share Posted August 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75187 Share on other sites More sharing options...
shocker-z Posted August 15, 2006 Author Share Posted August 15, 2006 Thanks effigy.. didnt see your post on my last post.. just tested it and working great! :)SOLVEDCheers for the help guysLiam Quote Link to comment https://forums.phpfreaks.com/topic/17619-formatting-postcode/#findComment-75207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.