使用 PHP 查找文件名中包含字符串或模式的目录中的所有文件
我正在尝试使用 PHP 列出目录(递归或非递归)中的文件,其中文件名与特定模式匹配。我对正则表达式从来都不太擅长,所以你能提供的任何帮助都会很棒。我可以搜索对返回的文件名进行文字检查,但我认为这不是一个好主意:)
更新 + 最终解决方案:1/18/2011 @ 8:06 PM
一旦我对正则表达式有了更多的了解,我就找到了另一种方法来完成我正在寻找的事情。诚然,我对正则表达式的使用情况感到非常沮丧,但多亏一位朋友把我拉到一边,用比我在在线指南中找到的更简单的术语解释了其中的一些内容,我现在才明白了一些。
该解决方案基本上检查具有“prefixone”或“prefixtwo”前导前缀的特定图像,同时还验证它是否是某种类型的图像(jpg、jpeg、png)并匹配以下任何格式。
根据您从 WordPress(我使用它的地方)传递的 slug,它将与该正则表达式匹配。下面是一个示例列表:
prefixone.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.002.jpeg
prefixtwo.123-abc._tag2._tag1.003.jpg
prefixone.123-abc._tag2.004.jpeg
prefixtwo.345-xyz._tag2._tag3._tag1.005.jpg
prefixtwo.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.001.png
prefixtwo.456-rst._tag1.001.png
所有这些文件都可能在我们的 opendir() 函数的文件列表中返回,如果 slug 匹配,那么这些文件中的任何一个都可能是匹配的。无论文件名中标记信息的顺序如何。
我希望这可以帮助其他正在努力解决正则表达式问题的用户。掌握窍门很痛苦,但是一旦您了解了一些基本知识,其余的事情就开始迅速落实到位,开始构建您自己的。
代码:
<?php
// create an array to hold directory list
$results = array();
// create a handler for the directory
$directory = $_SERVER['DOCUMENT_ROOT'].'/some/path/to/images/';
$handler = opendir($directory);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
// check with regex that the file format is what we're expecting and not something else
if(preg_match('#^(prefixone|prefixtwo)[^\s]*\.'.$wordpress-slug.'\.[^\s]+(\.(jpg|jpeg|png))#', $file)) {
// add to our file array for later use
$results[] = $file;
}
}
}
?>
我实际上并不需要递归,但网上确实有很多递归示例,这确实是我最不担心的。按模式进行内容隔离是此任务的核心,因此上述代码就足够了。
旁注:
对于那些昨天指出“已接受的评论”的人,我不知道我错过了这一点,我深表歉意。我今天过得很糟糕。抱歉,如果我似乎对这些评论对任何人大发雷霆。这是一个很棒的社区,我也很乐意尽我所能回馈社会。
I'm trying to list out files in a directory (recursive or non) with PHP where filename matches a certain pattern. I've never been too great with regex so any help you could offer would be great. I could search for a literal check on the filenames returned, but I think that's not such a great idea :)
Update + Final Solution: 1/18/2011 @ 8:06 PM
I found another way to do what I was looking for once I understood regex a bit more. Granted I was entirely frustrated about where I was with regex, I get a bit of it now thanks to a friend pulling me aside to explain some of this in simpler terms than I was finding in online guides.
This solution basically checks for a specific image(s) with a leading prefix of either "prefixone" or "prefixtwo", while also verifying that it's an image of a certain type (jpg, jpeg, png) and matches any of the following formats.
Depending on the slug you passed from Wordpress (where I was using this), it would match with that regular expression. Here is a sample listing:
prefixone.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.002.jpeg
prefixtwo.123-abc._tag2._tag1.003.jpg
prefixone.123-abc._tag2.004.jpeg
prefixtwo.345-xyz._tag2._tag3._tag1.005.jpg
prefixtwo.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.001.png
prefixtwo.456-rst._tag1.001.png
All of these files that may have potentially been returned in the file listing from our opendir() function, any of these could have been a match if the slug matched. Regardless of the ordering of tagging information in the filename.
I hope this helps another user struggling with regex. It's a pain to get the hang of but once you understand a few fundamental things, the rest starts to rapidly fall into place to start building your own.
Code:
<?php
// create an array to hold directory list
$results = array();
// create a handler for the directory
$directory = $_SERVER['DOCUMENT_ROOT'].'/some/path/to/images/';
$handler = opendir($directory);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
// check with regex that the file format is what we're expecting and not something else
if(preg_match('#^(prefixone|prefixtwo)[^\s]*\.'.$wordpress-slug.'\.[^\s]+(\.(jpg|jpeg|png))#', $file)) {
// add to our file array for later use
$results[] = $file;
}
}
}
?>
I didn't actually need recursive for this, but there really are plenty of recursive examples online and that was truly the least of my worries. Content isolation by pattern was the core of this task so the above code sufficed.
Sidenote:
To those that pointed out the "accepted comments" yesterday, I had no idea I was missing that and I apologize. I was having a bad day. Sorry if I seemed to snap at anyone about the comments. This is a great community and I'm happy to give back where I can, also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
glob
查找与模式匹配的路径名 或一个 GlobIterator。如果您需要递归 使用
RegexIterator
和RecursiveDirectoryIterator
.标记此 CW,因为该问题肯定是重复的,并且在使用搜索功能时您可以轻松找到上述所有问题的示例。请这样做。
Use
glob
to find pathnames matching a pattern or aGlobIterator
.If you need that to be recursive use a
RegexIterator
and aRecursiveDirectoryIterator
.Marking this CW because the question is a sure duplicate and you can easily find examples for all of the above when using the Search function. Please do so.