preg_match - windows filenames

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

    #1

    preg_match - windows filenames

    I am trying to work out the preg_match command to check windows
    filenames for illegal characters and I can not seem to get it right.
    The disallowed characters are \/:*?"<>|

    I can not figure out the correct syntax.

    Here's my best guess (which does not work). I'm using % to delimit the
    characters and I've added backslashes to escape most of the characters
    because they are also pattern modifiers.

    $matches = preg_match("%\\/:\*\?\"<>\|%", $filename);
    if ($matches > 0)
    echo "illegal filename - $filename";

    The above does not match the colon if I submit abc: as the filename.

    Any help would be much appreciated (and maybe then I can figure out how
    this works).

    --
    *************** **************
    Chuck Anderson • Boulder, CO

    Integrity is obvious.
    The lack of it is common.
    *************** **************
  • John Dunlop

    #2
    Re: preg_match - windows filenames

    Chuck Anderson wrote:
    [color=blue]
    > The disallowed characters are \/:*?"<>|[/color]

    [...]
    [color=blue]
    > %\\/:\*\?\"<>\|%[/color]

    Set them in a character class, otherwise the pattern will
    match only if *all* those characters occur _in the order you
    specified_. Alternatives is another way, but less efficient.

    %[\\\/:*?"<>|]%

    (Note the three backslashes instead of two. The backslash
    must be escaped twice - once because it's in a string and
    again for the regex engine. The other characters don't need
    escaping, except " if you double quote your string.)

    --
    Jock

    Comment

    • Chuck Anderson

      #3
      Re: preg_match - windows filenames

      John Dunlop wrote:
      [color=blue]
      >Chuck Anderson wrote:
      >
      >
      >[color=green]
      >>The disallowed characters are \/:*?"<>|
      >>
      >>[/color]
      >
      >[...]
      >
      >
      >[color=green]
      >>%\\/:\*\?\"<>\|%
      >>
      >>[/color]
      >
      >Set them in a character class, otherwise the pattern will
      >match only if *all* those characters occur _in the order you
      >specified_. Alternatives is another way, but less efficient.
      >
      > %[\\\/:*?"<>|]%
      >
      >(Note the three backslashes instead of two. The backslash
      >must be escaped twice - once because it's in a string and
      >again for the regex engine. The other characters don't need
      >escaping, except " if you double quote your string.)
      >
      >
      >[/color]
      Thank you, thank you. I hesitated asking because it seemed like I was
      looking for a "free handout."
      This clarifies things, though, and now I know more about regular
      expressions.

      --
      *************** **************
      Chuck Anderson • Boulder, CO

      Integrity is obvious.
      The lack of it is common.
      *************** **************

      Comment

      • Alvaro G Vicario

        #4
        Re: preg_match - windows filenames

        *** Chuck Anderson wrote/escribió (Tue, 02 Aug 2005 12:43:27 -0600):[color=blue]
        > I am trying to work out the preg_match command to check windows
        > filenames for illegal characters and I can not seem to get it right.
        > The disallowed characters are \/:*?"<>|
        >
        > I can not figure out the correct syntax.[/color]

        Hope this helps (not tested):

        $forbidden=preg _quote('\/:*?"<>', '/');
        preg_match('/[' . $forbidden . ']/', $filename);


        --
        -- Álvaro G. Vicario - Burgos, Spain
        -- http://bits.demogracia.com - Mi sitio sobre programación web
        -- Don't e-mail me your questions, post them to the group
        --

        Comment

        Working...