Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Jacobs

    Regular Expressions

    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
  • Mick White

    #2
    Re: Regular Expressions

    Andy Jacobs wrote:
    [color=blue]
    > 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.[/color]

    Grab the first 2 characters (if the entry is 2 or more characters)

    replace digit with "" (Nothing)

    Mick

    Comment

    • Michael Fesser

      #3
      Re: Regular Expressions

      .oO(Andy Jacobs)
      [color=blue]
      >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.[/color]

      if (preg_match('#^[a-z]{1,2}#i', $yourString, $result)) {
      // use $result[0] for search
      } else {
      // error handling
      }

      Micha

      Comment

      Working...