Jump to content


Photo

String manipultaion


  • Please log in to reply
4 replies to this topic

#1 UHTMilk

UHTMilk
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 March 2006 - 07:47 PM

Hiya there,

I have another newbie question for you good people out there. It's to do with splitting UK postcodes. I've got some of it worked out but wonder if you could lend any further advice. So far I've got: -

// Validate with this
if(!ereg("[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{1,2}", strtoupper($postcode))) {
}

// Get outbound section with this
$outpc=explode(" ",$dat['postcode']);
$outbound=$outpc[1]

I need to end up with three varaibles; the full code (rh1 1nn), the outbound code (rh1), and the area code from the outbound (rh). The first two were easy but I don't know of a way to get the third. As uk postcodes can be both 1 and 2 a-z characters before the number. So I don't know if there is anyway to extract from the string up to the first number.

I'd be grateful for any advice you could lend or maybe even a cleaner way of finding all three.

Thanks in advance,
Milk.

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 12 March 2006 - 08:05 PM

[!--quoteo(post=354268:date=Mar 12 2006, 07:47 PM:name=UHTMilk)--][div class=\'quotetop\']QUOTE(UHTMilk @ Mar 12 2006, 07:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hiya there,

I have another newbie question for you good people out there. It's to do with splitting UK postcodes. I've got some of it worked out but wonder if you could lend any further advice. So far I've got: -

// Validate with this
if(!ereg("[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{1,2}", strtoupper($postcode))) {
}

// Get outbound section with this
$outpc=explode(" ",$dat['postcode']);
$outbound=$outpc[1]

I need to end up with three varaibles; the full code (rh1 1nn), the outbound code (rh1), and the area code from the outbound (rh). The first two were easy but I don't know of a way to get the third. As uk postcodes can be both 1 and 2 a-z characters before the number. So I don't know if there is anyway to extract from the string up to the first number.

I'd be grateful for any advice you could lend or maybe even a cleaner way of finding all three.

Thanks in advance,
Milk.
[/quote]


Notice how in the strtoupper you are using $postcode, while in the explode statement you're using $dat['postcode'] .. i don't know the rest of your code, but that seems like it would be something to look in to.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 UHTMilk

UHTMilk
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 March 2006 - 08:11 PM

[!--quoteo(post=354276:date=Mar 12 2006, 08:05 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 12 2006, 08:05 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Notice how in the strtoupper you are using $postcode, while in the explode statement you're using $dat['postcode'] .. i don't know the rest of your code, but that seems like it would be something to look in to.
[/quote]

Hiya,

Thanks for getting back to me. The first one is validating from a form. The second example I used is where I'm pulling it from the db to compare. So it's no problems, but thanks for taking the time to look over it.

Milk.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,019 posts

Posted 12 March 2006 - 10:43 PM

try

$pc = 'rh1 1nn';
list ($outpc, $local) = explode (' ', $pc);
$area = $outpc{0};
if (!is_numeric($outpc{1})) {
     $area .= $outpc{1};
     $dist = substr($outpc, 2);
}
else 
     $dist = substr($outpc, 1);

echo $area, '-', $dist, '-', $local;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 UHTMilk

UHTMilk
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 March 2006 - 10:56 PM

Hiya,

Thank you again, you've helped me out before. When it comes to PHP you're a genius. It's worked fine. Thanks again mate,

Milk.



SOLVED




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users