PHP 中过滤文件名的正则表达式
我有代表文件路径的字符串。像这样的东西。
folder1/folderA/file1.flv folder2/folderB/file1.mp4 folder3/folderC/file1.jpg
我正在尝试编写一个基本上与有效扩展名列表匹配的表达式,例如 .flv
、.mp4
、.flv
我确信我离这里很远,但这就是我所拥有的 .[(\.flv/)|(\.wmv/)|(\.mp4)]
滑稽,我确信......但希望能展示要点。
I have strings that represent file paths. Something like this.
folder1/folderA/file1.flv folder2/folderB/file1.mp4 folder3/folderC/file1.jpg
I am trying to write an expression that basically matches against a list of valid extensions such as .flv
, .mp4
, .flv
I am sure I am way off here but here's what I have .[(\.flv/)|(\.wmv/)|(\.mp4)]
comical, I'm sure... but hopefully shows the gist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为正则表达式的替代方案,您可以使用
pathinfo()
它将可靠地为您提供路径中的文件扩展名。As an alternative to a regex, you could use
pathinfo()
which will reliably get you the file extension from a path.如果我正确解释它,您可能只需要类似的内容:
$
确保它在字符串/文件名的末尾匹配。括号中列出了替代方案,[.]
只是\.
的更好表示法。If I interpret it correctly, you probably just want something like:
The
$
ensures that it matches at the end of the string/filename. The alternatives are listed in the parens, and the[.]
is just a nicer notation for\.