PHP正则表达式的文件名 [英] PHP Regex for filename
问题描述
我使用户可以通过文本区域更改文件名,但是现在我遇到了正则表达式问题.
I made the user possible to change file names through a textarea, but now I'm having a regex problem.
对于Windows 7,我看到文件名只不允许使用这些字符:
As I see for Windows 7, these characters only are not allowed for filenames:
\ / : * ? < > |
但是我很固执,也许也很明智,选择将正则表达式最小化为仅这些特殊字符:
But I stubbornly, perhaps also wisely, choose to minimize the regex to ONLY these special characters:
- _ .
所有其他的都应该剪掉.
All the others should have to be cut.
有人可以帮我解决这个正则表达式吗?
Can someone help me out with this regex?
preg_replace除了以下所有内容:
A-Za-z0-9
和- _ .
preg_replace all but:
A-Za-z0-9
and - _ .
我仍然真的不了解它.
推荐答案
preg_replace('/[^A-Za-z0-9 _ .-]/', '', $filename);
[]是字符类,而^否定它.因此,它实际上与该组中的字符匹配.
The [] is a character class and the ^ negates it. So it literally matches anything other than the chars in that group.
请注意,-
在末尾,因为如果在其他地方(例如, 0-9
Note that -
is at the end, as it is a special range character if used elsewhere, e.g. 0-9
这篇关于PHP正则表达式的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!