simple eregi not working (PEBCAK)

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

    simple eregi not working (PEBCAK)

    I'm trying to make sure a search text field has only numbers, letters, or
    spaces.
    I started with this:

    $pattern = "^([ 0-9a-z])";
    if (eregi($pattern , $search_txt)) {

    And that works so long as the 1st character isn't a number or letter,
    otherwise it allows the whole string.
    I did some newsgroup looking, and everything I've found has been for very
    complex checks like e-mail addresses.
    The simplest example I could find is:

    $pattern = "[a-z0-9_\-]+";
    if (eregi($pattern , $search_txt)) {

    Which evidently also checks against only 1 letter searches, which is also
    useful. But it still allows searches that contain both valid and invalid
    characters. It only refuses patters that are only invalid.

    This is something that's so rediculously simple...but is still beyond me. =/
    Any suggestions?

    Thanks,
    Liam


  • Andy Hassall

    #2
    Re: simple eregi not working (PEBCAK)

    On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <deja@celticbea r.com> wrote:
    [color=blue]
    >I'm trying to make sure a search text field has only numbers, letters, or
    >spaces.
    >I started with this:
    >
    > $pattern = "^([ 0-9a-z])";
    > if (eregi($pattern , $search_txt)) {
    >
    >And that works so long as the 1st character isn't a number or letter,
    >otherwise it allows the whole string.[/color]

    You have:

    ^([ 0-9a-z])

    ... which only looks at the first character. You probably want:

    ^[ 0-9a-z]+$

    (matches if string consists entirely of good characters)

    Or

    [^ 0-9a-z]

    (matches if string contains at least one bad character)

    ^ in a pattern matches start of line.
    ^ in a character class inverts the character class.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • LRW

      #3
      Re: simple eregi not working (PEBCAK)

      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:9u04r05tjh volm9obfre9t4vj trgglaie0@4ax.c om...[color=blue]
      > On Sat, 04 Dec 2004 18:19:51 GMT, "LRW" <deja@celticbea r.com> wrote:
      >[color=green]
      >>I'm trying to make sure a search text field has only numbers, letters, or
      >>spaces.
      >>I started with this:
      >>
      >> $pattern = "^([ 0-9a-z])";
      >> if (eregi($pattern , $search_txt)) {
      >>
      >>And that works so long as the 1st character isn't a number or letter,
      >>otherwise it allows the whole string.[/color]
      >
      > You have:
      >
      > ^([ 0-9a-z])
      >
      > ... which only looks at the first character. You probably want:
      >
      > ^[ 0-9a-z]+$
      >[/color]

      Yep that did it. Sheesh, how simple! I can't believe I didn't get the
      parentheses and dollarsign usage from what I looked at.
      [color=blue]
      > ^ in a pattern matches start of line.
      > ^ in a character class inverts the character class.
      >[/color]

      More good info to know. Thanks!
      Liam


      Comment

      • Michael Fesser

        #4
        Re: simple eregi not working (PEBCAK)

        .oO(LRW)
        [color=blue]
        >Yep that did it. Sheesh, how simple! I can't believe I didn't get the
        >parentheses and dollarsign usage from what I looked at.[/color]

        And now you should consider using preg_* instead of the old ereg*
        functions. They are much more powerful and often faster.

        Micha

        Comment

        Working...