tags:

views:

219

answers:

2

I want to clean the filenames of all uploaded files. I want to remove all characters except periods, letters and numbers. I'm not good with regex so I thought I would ask here. Can someone point me to a helpful site or show me how to put this together? I'm using PHP.

+1  A: 
s/[^.a-zA-Z\d]//g

(This is a Perl expression of how to use the RegExp. In PHP you do:

$output = preg_replace('/[^.a-zA-Z\d]/', '', $input);
KennyTM
Thanks Kenny. Will this also match periods?
Timay
@Timay: The `^` means "any characters except". The `.a-zA-Z\d` after that means "period, alphabets and numbers". So it'll match all characters which are not period, alphabets and numbers, then remove them.
KennyTM
Thank you Kenny. I'm going to plug this in now.
Timay
Timay
+2  A: 
$newfilename=preg_replace('/[^a-zA-Z0-9.]/','',$filename);
S.Mark
Thanks. This works. I also forgot to add an underscore. This works but is it optimal? [^a-zA-Z0-9.|_]
Timay
@Timay: If you want to match the underscore, use `[^a-zA-Z0-9._]`, or simpler, `[^.\w]`.
KennyTM
Wow.. Just those 4 characters will match everything except numbers, letters?
Timay
@Timay: Probably you should read more about regexp (http://www.regular-expressions.info/).
KennyTM
:) Yeah, I agree. I seldom use it but it certainly couldn't hurt. Thank you for your help Kenny.
Timay
Is the backslash an escape character or part of a flag or switch?
Timay
\ in \w is not escape charcter, its shorthand form \w=a-zA-Z0-9_ http://www.regular-expressions.info/reference.html
S.Mark
Thanks for the explanation Mark. Kenny, I see that you updated your original answer explaining that what you gave me was a perl expression. I'm sorry I missed that. The regex is exactly the same.
Timay
@S.Mark: He wants to keep the `.` too.
KennyTM
right, i have removed extra comment for \W
S.Mark
Exactly. I've settled on using [^.\w] This seems to work well.
Timay