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
2010-01-30 07:04:45
Thanks Kenny. Will this also match periods?
Timay
2010-01-30 07:06:01
@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
2010-01-30 07:07:27
Thank you Kenny. I'm going to plug this in now.
Timay
2010-01-30 07:09:05
Timay
2010-01-30 07:11:15
Thanks. This works. I also forgot to add an underscore. This works but is it optimal? [^a-zA-Z0-9.|_]
Timay
2010-01-30 07:14:18
@Timay: If you want to match the underscore, use `[^a-zA-Z0-9._]`, or simpler, `[^.\w]`.
KennyTM
2010-01-30 07:15:52
Wow.. Just those 4 characters will match everything except numbers, letters?
Timay
2010-01-30 07:17:56
@Timay: Probably you should read more about regexp (http://www.regular-expressions.info/).
KennyTM
2010-01-30 07:19:03
:) Yeah, I agree. I seldom use it but it certainly couldn't hurt. Thank you for your help Kenny.
Timay
2010-01-30 07:20:16
\ in \w is not escape charcter, its shorthand form \w=a-zA-Z0-9_ http://www.regular-expressions.info/reference.html
S.Mark
2010-01-30 07:24:29
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
2010-01-30 07:27:37