5

I'd like to validate file paths with a regular expression. So far I've come up with this:

preg_match('/[^A-Za-z0-9\.\/\\\\]/', $string);

so it will return 1 if the string has any other characters than A-Z, a-z, 0-9, dot, \ and /

How can I make it so it also returns 1 if there are more than two dots in the string, or if the dot is at the end of the string?

And how can I allow : but only if it's present as the 2nd character and followed by \ or /. For example c:\file should be valid

1
  • Just try to access the file and catch the error. Don't bother validating. Commented May 9, 2013 at 18:36

2 Answers 2

9

For the first two requirements:

preg_match('/[^A-Za-z0-9\.\/\\\\]|\..*\.|\.$/', $string);

The \..*\. will match if there are more than two dots. \.$ will match if there is a dot at the end. By separating each of these portions (including your original regex) with a | it will make it so the regex will match any one of the expressions (this is called alternation).

The last requirement is a little tricky, since if I understand correctly you need this to return 1 if there is a :, unless the only colon is the second character and it is followed by a \ or /. The following regex (as a PHP string) should do that:

'/:(?!(?<=^[a-zA-Z]:)[\/\\\\])/'

Or combined with the other regex (note that we also have to add : to the first character class):

preg_match('/[^A-Za-z0-9\.\/\\\\:]|\..*\.|\.$|:(?!(?<=^[a-zA-Z]:)[\/\\\\])/', $string);

Here is the explanation for that last piece:

:                  # match a ':'
(?!                # but fail if the following regex matches (negative lookahead)
   (?<=^[a-zA-Z]:)   # the ':' was the second character in the string
   [\/\\\\]          # the next character is a '/' or '\'
)                  # end lookahead
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! last part doesn't seem to work though. Could I negate this expression: ^[A-Za-z]:[\.\/\\\\] ? So if this doesn't match it should fail
@Alex See my edit, the issue with the old version was that : needs to be added to the character class from your original regex or every : will cause the regex to match. :(?!(?<=^[a-zA-Z]:)[\/\\\\]) is the negation of the regex from you comment (except for the ., do you really want C:.test to be valid?).
Nope, but I added the dot in the list by mistake :) thanks again :P
1

If this is for files on your server, this is what realpath is for. It will resolve references to . and .. and check if the file exists at that path. If the file doesn't exist it returns false, if it does exist it returns the new path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.