【问题标题】:Finding files with a certain string查找具有特定字符串的文件
【发布时间】:2013-01-13 08:33:21
【问题描述】:

我在这里是一个真正的菜鸟。我想知道是否有人可以给我一段代码,该代码将使用用户指定的字符串查找某个目录中的所有文件以及该目录的子目录。并且如果可能限制要搜索的文件类型,例如*.php

我 99% 可能会使用 RecursiveDirectoryIterator、preg_match 或 GLOB,但我真的是 php 新手,对这些函数几乎一无所知。

这种代码当然很容易用UNIX 命令完成,但 PHP 我有点卡住了(需要 PHP 而不是 unix 解决方案)。非常感谢我能从你们那里得到的所有帮助!

编辑:似乎我可能让你们中的一些人感到困惑。我希望该字符串位于文件内部而不是文件名中。

【问题讨论】:

  • 你是在 Windows 服务器还是 Unix 服务器上?你标记了两者。
  • 看看有没有帮助:redips.net/php/find-files-with-php
  • 我在 unix 上,但我只是想确保我需要它们同时工作,即 PHP 解决方案

标签: php windows unix


【解决方案1】:

你可以很容易地做到这一点。

// 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);

【讨论】:

  • 结构看起来不错,但我得到:解析错误:语法错误,第 21 行 /usr/www/virtual/myfile.php 中的意外“{”。此外,此搜索是否递归?我正在寻找已定义目录的所有子目录
  • 哎呀,忘记了右括号 - 更新了答案。不,此实现不会递归搜索。
  • 当我执行时,我得到输出:Array ( )
  • 抱歉,忘记将文件名设为小写。假设您的搜索字符串存在于您正在搜索的目录中,它现在应该可以工作了。
  • 嗯,很奇怪。当我用一个词搜索某些东西时+例如“扩展”它输出数组()但是当我搜索字符串“a”时它输出很好:s
【解决方案2】:

仅在 FreeBSD 上测试...

在传递目录的所有文件中查找string(仅限*nix):

<?php

$searchDir = './';
$searchString = 'a test';

$result = shell_exec('grep -Ri "'.$searchString.'" '.$searchDir);

echo '<pre>'.$result.'</pre>';

?>

仅使用 PHP 在传递目录的所有文件中查找 string(不建议在大量文件中使用):

<?php

$searchDir = './';
$searchExtList = array('.php','.html');
$searchString = 'a test';

$allFiles = everythingFrom($searchDir,$searchExtList,$searchString);

var_dump($allFiles);

function everythingFrom($baseDir,$extList,$searchStr) {
    $ob = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir), RecursiveIteratorIterator::SELF_FIRST);
    foreach($ob as $name => $object){
        if (is_file($name)) {
            foreach($extList as $k => $ext) {
                if (substr($name,(strlen($ext) * -1)) == $ext) {
                    $tmp = file_get_contents($name);
                    if (strpos($tmp,$searchStr) !== false) {
                        $files[] = $name;
                    }
                }
            }
        }
    }
    return $files;
}
?>

编辑:基于更多细节的更正。

【讨论】:

  • 对不起,我在哪里输入我的字符串?
  • 您需要将string 传递给everythingFrom('./','.php'); 因此,将'./' 替换为string。 (示例是搜索当前的basedir,即./)编辑:更新答案以使其更清晰。
  • 是否使用当前目录作为定义目录?我正在寻找类似 $directory = '/var/www/' $searchString = 'mystring';等等
  • 好的,对答案进行了一些更改。立即尝试。
  • 好的,我刚刚测试过。当我有 $searchString = 'a';它只返回我定义的目录中的所有文件/目录/子目录。当我有 $searchString = 'myotherproperstring' 时,它返回“NULL”,即使该字符串位于正确目录中的文件中。我在使用 $searchString = 'a'; 时注意到的一件事是它正在查找所有子目录,这很好。不知道为什么它不能识别正确的字符串:S p.s.新代码搜索所有文件类型,而不仅仅是 *.php?
【解决方案3】:

我找到了一个小文件来搜索文件夹中的字符串:

在此处下载文件from。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多