3

anyone can help me with a preg_match? I'd like to use php's preg_match to determine if an input is a valid filename or not (only the filename + file extension, not the full path). General rules:

1) filename = a-z, A-Z, 0-9
2) extension = 3 or 4 letters

Thank you!

4
  • @stereofrog strings with trailing new lines will not be matched by any of the given regexes as $ will match before intermediate new line characters only in multiline mode. regular-expressions.info/anchors.html Commented May 3, 2010 at 9:24
  • @Amarghosh did you even read the page that you linked to? :/ Even though \Z and $ only match at the end of the string (when the option for the caret and dollar to match at embedded line breaks is off), there is one exception. If the string ends with a line break, then \Z and $ will match at the position before that line break, rather than at the very end of the string. Commented May 3, 2010 at 9:42
  • @stereofrog, abiding by the OPs "general rules" the pattern should be either /^[a-zA-Z0-9]+\.[a-zA-Z]{3,4}$/D or /^[a-z0-9]+\.[a-z]{3,4}$/Di or /\A[a-zA-Z0-9]+\.[a-zA-Z]{3,4}\z/ or /\A[a-z0-9]+\.[a-z]{3,4}\z/i or something along those lines. Commented May 3, 2010 at 9:46
  • @salathe You're right: I didn't read the "Strings Ending with a Line Break" paragraph - I read first four paras and was happy :( But again, who'll end a file name with a new line (assuming it is entered by user in a html text input) ;) Commented May 3, 2010 at 10:24

5 Answers 5

5

Try this:

preg_match('/^[a-zA-Z0-9]+\.[a-zA-Z]{3,4}$/', $filename)
Sign up to request clarification or add additional context in comments.

Comments

4

You can do:

if(preg_match('#^[a-z0-9]+\.[a-z]{3,4}$#i',$filename)) {
        echo "Valid";
}else{
        echo "not Valid";
}

3 Comments

$filename = "foobar.baz\n";
@salathe $ matches before new lines only in multiline mode (with m flag).
@Amarghosh, no it matches before new lines unless told not to (with the D modifier)
1
/^[a-zA-Z0-9]+\.[a-zA-Z]{3,4}$/

If you want to enforce min/max length for the file name part:

//minimum 4 characters and a maximum of 8 characters

/^[a-zA-Z0-9]{4,8}\.[a-zA-Z]{3,4}$/

Comments

1
^\w+\.\w{3,4}$

Should work.

3 Comments

\w includes underscores too.
What if I want to upload a .torrent file :(
The question says that the extension should be 3 or 4 characters long. So .torrent files do not match the criteria.
0

Try this:

$filename1 = "file_16may25_001818.csv";
$filename2 = "file_16may25_001818";
$filename3 = "file.csv";
$filename4 = "file.txt";


echo "<br />filename1=>".preg_match('/^file(.*).csv/', $filename1);
echo "<br />filename2=>".preg_match('/^file(.*).csv/', $filename2);
echo "<br />filename3=>".preg_match('/^file(.*).csv/', $filename3);
echo "<br />filename4=>".preg_match('/^file(.*).csv/', $filename4);

?>

Output:

filename1=>1
filename2=>0
filename3=>1
filename4=>0

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.