我在这里是个真正的菜鸟.我想知道是否有人可以给我一段代码,它将找到某个目录中的所有文件以及该目录的子目录和用户指定的字符串.如果可能的话,限制搜索文件类型,例如*.php
我99%它可能使用RecursiveDirectoryIterator,preg_match或者GLOB我是php的新手,对这些功能几乎没有任何了解.
这种代码使用UNIX命令肯定很容易,但PHP有点卡住(需要PHP而不是unix解决方案).非常感谢能从你们这里得到的所有帮助!
编辑:似乎我可能会让你们有些困惑.我希望该字符串在文件中不在FILENAME中.
Aus*_*rst 10
你可以很容易地完成这个任务.
// string to search in a filename.
$searchString = 'myFile';
// all files in my/dir with the extension
// .php
$files = glob('my/dir/*.php');
// array populated with files found
// containing the search string.
$filesFound = array();
// iterate through the files and determine
// if the filename contains the search string.
foreach($files as $file) {
$name = pathinfo($file, PATHINFO_FILENAME);
// determines if the search string is in the filename.
if(strpos(strtolower($name), strtolower($searchString))) {
$filesFound[] = $file;
}
}
// output the results.
print_r($filesFound);
Run Code Online (Sandbox Code Playgroud)