Matching a filename

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

    #1

    Matching a filename

    I wanna match the incorrect filename with the next regular expression
    but it doesn´t work. Why? I don´t know.

    if (ereg("[^A-Za-z0-9\.]+", $str))
    return false;
    else
    return true;

    I wanna allow only letters (A-Za-z) and numbers (0-9) and of course a
    dot for the extension.

    Have anybody any suggestion or idea? Any idea would be appreciated!!


    Fran García
  • Tim Van Wassenhove

    #2
    Re: Matching a filename

    On 2005-02-25, Fran Garcia <fgarcia@ua.e s> wrote:[color=blue]
    > I wanna match the incorrect filename with the next regular expression
    > but it doesn´t work. Why? I don´t know.
    >
    > if (ereg("[^A-Za-z0-9\.]+", $str))
    > return false;
    > else
    > return true;[/color]

    return preg_match("#^[A-Za-z0-9\.]+$#", $str)

    but what about $string = "C:\\My Documents" ?




    --
    Met vriendelijke groeten,
    Tim Van Wassenhove <http://www.timvw.info>

    Comment

    • Chung Leong

      #3
      Re: Matching a filename

      "Tim Van Wassenhove" <timvw@users.so urceforge.net> wrote in message
      news:3891bhF5kv 67lU1@individua l.net...[color=blue]
      > On 2005-02-25, Fran Garcia <fgarcia@ua.e s> wrote:[color=green]
      > > I wanna match the incorrect filename with the next regular expression
      > > but it doesn´t work. Why? I don´t know.
      > >
      > > if (ereg("[^A-Za-z0-9\.]+", $str))
      > > return false;
      > > else
      > > return true;[/color]
      >
      > return preg_match("#^[A-Za-z0-9\.]+$#", $str)
      >
      > but what about $string = "C:\\My Documents" ?[/color]

      preg_match('/^[\w\.]+$/', $str) is more compact; covers the underscore as
      well.


      Comment

      Working...