Regular expression to compare two variables similar to SQL: field LIKE 'value%'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bissatch@yahoo.co.uk

    Regular expression to compare two variables similar to SQL: field LIKE 'value%'

    I am trying to compare two variable but using regular expression:

    $access = "glasgow"

    $areaid = "glasgow-westend-byers_rd"

    using the two variables, I would like the areaid variable to be
    compared with the access variable. the regular expression function will
    return true if $access matches the start of $areaid. So the two above
    would return true

    The following would also return true:

    $access = "glasgow-westend"

    $areaid = "glasgow-westend-byers_rd"

    or

    $areaid = "glasgow-westend-greatwestern_rd "

    However, the following should return false though:

    $access = "glasgow"

    $areaid = "stirling-glasgow_rd"

    Although the value for $access, 'glasgow', appears in the $areaid it
    does not appear at the start.


    If I were to do this in SQL it would be something like:

    $select = "SELECT * FROM areas WHERE areaid LIKE '" . $access . '%"

    Note the use of '%' at the end of the compare LIKE field

    Unfortunetely I have never been able to properly get my head round
    regular expressions.

    Any help?

    Cheers, burnsy

  • Andy Jeffries

    #2
    Re: Regular expression to compare two variables similar to SQL: fieldLIKE 'value%'

    bissatch@yahoo. co.uk wrote:[color=blue]
    > I am trying to compare two variable but using regular expression:
    >
    > $access = "glasgow"
    >
    > $areaid = "glasgow-westend-byers_rd"
    >
    > using the two variables, I would like the areaid variable to be
    > compared with the access variable. the regular expression function will
    > return true if $access matches the start of $areaid. So the two above
    > would return true[/color]

    if (ereg("^$access ", $areaid)) {
    // ... do something
    }

    Cheers,


    Andy

    Comment

    • John Dunlop

      #3
      |OT| Byres Road, Glasgow (Scotland)

      bissatch wrote:
      [color=blue]
      > $areaid = "glasgow-westend-byers_rd"[/color]

      on a sidenote, bissatch, the street between Dumbarton Road and Great
      Western Road spells its name with an 're', does it not? it runs through
      what was once called the Byres of Partick ('byre' = shed for keeping
      cattle in). which begs the question, is there more heifers there now (on
      a Friday night) than there was then?

      --
      Jock

      Comment

      Working...