This is a discussion on Regular Expressions within the PHP Language forums, part of the PHP Programming Forums category; Hi all Hopefully a nice easy one :o) I've got a table with a list of 'installers'. I have ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi all
Hopefully a nice easy one :o) I've got a table with a list of 'installers'. I have a form for visitors to find an installer in their area searchning on post code. Let's say we have installers in: RG (Reading) G (Glasgow) The page tells the vistor to type in the first letter(s) of their post code. So, if a visitor types in their postcode as RG23 1TQ, it works. If they type in G23 1TQ then it doesn't. If they type in just G - as instructed - then it works. I've just chopped off all but the first 2 digits of whatever they type in (RG or G2). What I would like to do is (In pigeon PHP): If characters one and two are letters, use this for the search. If character one is a letter and character two is a number then just chop the first character off and use it for the search. elseif display error. What I can't get my head round is the logic and regular expressions behind this. Could someone throw me a lifeline please. Cheers Andy |
|
|||
|
Andy Jacobs wrote:
> Hi all > > Hopefully a nice easy one :o) > > I've got a table with a list of 'installers'. I have a form for > visitors to find an installer in their area searchning on post code. > > Let's say we have installers in: > > RG (Reading) > G (Glasgow) > > The page tells the vistor to type in the first letter(s) of their post > code. So, if a visitor types in their postcode as RG23 1TQ, it works. > If they type in G23 1TQ then it doesn't. If they type in just G - as > instructed - then it works. > > I've just chopped off all but the first 2 digits of whatever they type > in (RG or G2). What I would like to do is (In pigeon PHP): > > If characters one and two are letters, use this for the search. > > If character one is a letter and character two is a number then just > chop the first character off and use it for the search. > > elseif display error. > > What I can't get my head round is the logic and regular expressions > behind this. Grab the first 2 characters (if the entry is 2 or more characters) replace digit with "" (Nothing) Mick |
|
|||
|
.oO(Andy Jacobs)
>I've just chopped off all but the first 2 digits of whatever they type >in (RG or G2). What I would like to do is (In pigeon PHP): > >If characters one and two are letters, use this for the search. > >If character one is a letter and character two is a number then just >chop the first character off and use it for the search. > >elseif display error. if (preg_match('#^[a-z]{1,2}#i', $yourString, $result)) { // use $result[0] for search } else { // error handling } Micha |