By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,822 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,822 IT Pros & Developers. It's quick & easy.

String comparing

P: n/a
AJ
Hi All

Just a quickie. A form element for a search will contain either a town or a
post code. Is there any way we can check the input to see if the element
contains a number (in which case it will be a postcode) or not (in which
case it will be a town).

Cheers

Andy
Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Thu, 08 Apr 2004 15:22:11 +0000, AJ wrote:
Hi All

Just a quickie. A form element for a search will contain either a town or a
post code. Is there any way we can check the input to see if the element
contains a number (in which case it will be a postcode) or not (in which
case it will be a town).

Cheers

Andy

Yup =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a
How about:

if(is_int($var))
{
// must be a zip
}
else
{
// must be a town
}

Hope this helps,

PENN

AJ wrote:
Hi All

Just a quickie. A form element for a search will contain either a town or a
post code. Is there any way we can check the input to see if the element
contains a number (in which case it will be a postcode) or not (in which
case it will be a town).

Cheers

Andy


Jul 17 '05 #3

P: n/a
"AJ" <no****@redcatmedia.net> schreef op do, 08 apr 2004 15:22:11 GMT in
news:c5**********@sparta.btinternet.com:
Hi All

Just a quickie. A form element for a search will contain either a
town or a post code. Is there any way we can check the input to see
if the element contains a number (in which case it will be a postcode)
or not (in which case it will be a town).


http://be.php.net/manual/nl/function.is-numeric.php

you gotta love it! ;-)

<T!M> aka wEEdpEckEr
Jul 17 '05 #4

P: n/a

"AJ" <no****@redcatmedia.net> wrote in message
news:c5**********@sparta.btinternet.com...
Hi All

Just a quickie. A form element for a search will contain either a town or a post code. Is there any way we can check the input to see if the element
contains a number (in which case it will be a postcode) or not (in which
case it will be a town).

Cheers

Andy


Sorry everyone else, this guy's posting from btinternet.com, so he's
British - alphanumeric postcodes.

Regular expression time.

if(preg_match('/\w+^$/',$field)) {
......
}

Etc.

Garp
Jul 17 '05 #5

P: n/a
On Thu, 08 Apr 2004 19:01:38 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:
"AJ" <no****@redcatmedia.net> wrote in message
news:c5**********@sparta.btinternet.com...
Hi All

Just a quickie. A form element for a search will contain either a town or a
post code. Is there any way we can check the input to see if the element
contains a number (in which case it will be a postcode) or not (in which
case it will be a town).

Cheers

Andy


Sorry everyone else, this guy's posting from btinternet.com, so he's
British - alphanumeric postcodes.

Regular expression time.

if(preg_match('/\w+^$/',$field)) {
.....
}


UK postcodes (at least all the ones I can think of) are of the form 'WX12 3YZ'
with one or two letters and numbers in the first group, and the second one
always being single digit and two letters. So maybe:

[A-Z]{1,2}[0-9]{1,2} [0-9][A-Z]{2}

There's bound to be exceptions to this though.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #6

P: n/a

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:9q********************************@4ax.com...
On Thu, 08 Apr 2004 19:01:38 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:
"AJ" <no****@redcatmedia.net> wrote in message
news:c5**********@sparta.btinternet.com...
Hi All

Just a quickie. A form element for a search will contain either a town or a post code. Is there any way we can check the input to see if the element contains a number (in which case it will be a postcode) or not (in which case it will be a town).

Cheers

Andy
Sorry everyone else, this guy's posting from btinternet.com, so he's
British - alphanumeric postcodes.

Regular expression time.

if(preg_match('/\w+^$/',$field)) {
.....
}


UK postcodes (at least all the ones I can think of) are of the form 'WX12

3YZ' with one or two letters and numbers in the first group, and the second one
always being single digit and two letters. So maybe:

[A-Z]{1,2}[0-9]{1,2} [0-9][A-Z]{2}

There's bound to be exceptions to this though.


Looks good, but remember that some people enter lowercase, drop the space
altogether, only enter the first part, etc. If it's just a digit he wants,
just '/.*\d+.*/' will do it I suppose.

I'd probably just do a town check first, then resort to postcode lookups if
that failed (or vice versa) if it's not too expensive to do so.

Garp
Jul 17 '05 #7

P: n/a
"Penn Markham" <pe**********@nospam4me.verizon.net> wrote in message
news:c5**********@solaris.cc.vt.edu...
How about:

if(is_int($var))
{
// must be a zip
}
else
{
// must be a town
}

Hope this helps,


Nope, it doesn't. Is_int() will always return false in this case. The
function you need is is_numeric(). Actually, that function doesn't work
either, since it'd return false when something like "20740-1001" is passed.

This is perhaps better:

if((int) $var > 0) {
// zip
}
else {
// town
}

Assuming no town or city name begins with a digit.
Jul 17 '05 #8

P: n/a
On Thu, 8 Apr 2004 20:10:36 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
A form element for a search will contain either a town or a
post code.


This is perhaps better:

if((int) $var > 0) {
// zip
}
else {
// town
}

Assuming no town or city name begins with a digit.


Post codes always start with a letter here in the UK - they're quite different
from ZIP codes.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #9

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:vm********************************@4ax.com...
On Thu, 8 Apr 2004 20:10:36 -0400, "Chung Leong" <ch***********@hotmail.com> wrote:
A form element for a search will contain either a town or a
post code.
This is perhaps better:

if((int) $var > 0) {
// zip
}
else {
// town
}

Assuming no town or city name begins with a digit.


Post codes always start with a letter here in the UK - they're quite

different from ZIP codes.


The OP seems to imply that the database contain only info in America.
Jul 17 '05 #10

P: n/a
In article <%S*********************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
If it's just a digit he wants,
just '/.*\d+.*/' will do it I suppose.


Even simpler, just '/\d/' will do.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #11

P: n/a

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <%S*********************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
If it's just a digit he wants,
just '/.*\d+.*/' will do it I suppose.


Even simpler, just '/\d/' will do.

JP


Yup, but I find the simple way doesn't always make the point when answering
a question. 8)

Garp
Jul 17 '05 #12

P: n/a

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:It********************@comcast.com...
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:vm********************************@4ax.com...
On Thu, 8 Apr 2004 20:10:36 -0400, "Chung Leong"

<ch***********@hotmail.com>
wrote:
>A form element for a search will contain either a town or a
>post code.

This is perhaps better:

if((int) $var > 0) {
// zip
}
else {
// town
}

Assuming no town or city name begins with a digit.


Post codes always start with a letter here in the UK - they're quite

different
from ZIP codes.


The OP seems to imply that the database contain only info in America.


Where? In
>A form element for a search will contain either a town or a
>post code.

? Headers are the giveaway:

Path:
news-text.cableinet.net!news-fe1!news-hub.cableinet.net!blueyonder!proxad.ne
t!proxad.net!fr.ip.ndsoftware.net!dedekind.zen.co. uk!zen.net.uk!demorgan.zen
..co.uk!194.72.9.35.MISMATCH!news-peer1!btnet-feed5!btnet!news.btopenworld.co
m!not-for-mail
From: "AJ" <no****@redcatmedia.net>
Newsgroups: comp.lang.php
Subject: String comparing
Date: Thu, 8 Apr 2004 15:22:11 +0000 (UTC)
Organization: BT Openworld
Lines: 12
Message-ID: <c5**********@sparta.btinternet.com>
NNTP-Posting-Host: host81-130-53-47.in-addr.btopenworld.com
X-Trace: sparta.btinternet.com 1081437731 5872 81.130.53.47 (8 Apr 2004
15:22:11 GMT)
X-Complaints-To: ne*************@lists.btinternet.com
NNTP-Posting-Date: Thu, 8 Apr 2004 15:22:11 +0000 (UTC)
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MSMail-Priority: Normal
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
X-Received-Date: Thu, 08 Apr 2004 16:23:02 BST (news-text.cableinet.net)
Xref: news-text.cableinet.net comp.lang.php:58444

BT (British Telecom) all the way.

Garp
Jul 17 '05 #13

P: n/a
Andy Hassall wrote:
UK postcodes (at least all the ones I can think of) are of the form 'WX12 3YZ'
with one or two letters and numbers in the first group, and the second one
always being single digit and two letters.


According to (beware the silly, inconsiderate URL and proprietary file
format -- par for the course with .govs, I'm sorry to say):

<http://www.lsc.gov.uk/NR/rdonlyres/e...m5pxsa2w2z7tk2
3kdrt2rth53fd4yycetfbddoauw7moi7ynlbnx4yl6ba/AnnexCPostcodeInfo.doc>

Cf. http://www.evoxfacilities.co.uk/evoxps.htm

in general UK postcode format is as you describe: six to eight
characters long (including the single space); the outward code (the
part before the space) is two to four characters long and the inward
code (the part after the space) is three characters long. The inward
code is simple to match as it consists of a single decimal digit
followed by two letters (excluding C, I, K, M, O and V); the outward
code is more complex because the District code (the second part of the
outward code) is usually numeric (decimal digits) but can be
alphanumeric ([A-Z0-9]).

One example given is: EC1A 1BB, where the Area code is EC, the
District code is 1A, the Sector code is 1 and the Walk code is BB.

So, to match and capture the components of a UK postcode with a
regular expression (PCRE):

([A-Z]{1,2})(\d[A-Z\d]?) (\d)([ABD-HJLNP-UW-Z]{2})

--
Jock
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.