簡體   English   中英

在php中遞歸搜索所有目錄中的字符串數組

[英]Recursively search all directories for an array of strings in php

我是 PHP 編碼的新手,在這里我正在尋找在所有目錄上遞歸搜索字符串數組的最快方法。

我這樣做

$contents_list = array("xyz","abc","hello"); // this list can grow any size
$path = "/tmp/"; //user will give any path which can contain multi level sub directories

$dir = new RecursiveDirectoryIterator($path);

foreach(new RecursiveIteratorIterator($dir) as $filename => $file) {
    $fd = fopen($file,'r');
    if($fd) {
        while(!feof($fd)) {
            $line = fgets($fd);
            foreach($contents_list as $content) {
                if(strpos($line, $content) != false) {
                    echo $line."\n";
                }
            }         
        }
    }
    fclose($fd);
}

在這里,我遞歸地遍歷所有目錄,然后在每個文件上再次遍歷內容數組以進行搜索。

有沒有更好的方法來進行搜索? 請建議更快的替代方案。

謝謝

如果您被允許在您的環境中執行 shell 命令(並假設您在 *nix 上運行您的腳本),您可以遞歸調用本機 grep 命令。 這會給你最快的結果。

$contents_list = array("xyz","abc","hello");
$path = "/tmp/";
$pattern = implode('\|', $contents_list) ;
$command = "grep -r '$pattern' $path";
$output = array();
exec($command, $output);
foreach ($output as $match) {
    echo $match . '\n';
}

如果disable_functions指令有效並且您無法調用 grep,則可以將您的方法與RecursiveDirectoryIterator並逐行讀取文件,在每一行上使用 strpos 。 請注意strpos需要嚴格的相等性檢查(使用!== false而不是!= false ),否則您將跳過一行開頭的匹配項。

稍微快一點的方法是使用 glob 遞歸獲取文件列表,並立即讀取這些文件,而不是逐行掃描它們。 根據我的測試,這種方法會讓你比你的方法有 30-35% 的時間優勢。

function recursiveDirList($dir, $prefix = '') {
    $dir = rtrim($dir, '/');
    $result = array();

    foreach (glob("$dir/*", GLOB_MARK) as &$f) {
        if (substr($f, -1) === '/') {
            $result = array_merge($result, recursiveDirList($f, $prefix . basename($f) . '/'));
        } else {
            $result[] = $prefix . basename($f);
        }
    }

    return $result;
}

$files = recursiveDirList($path);
foreach ($files as $filename) {

    $file_content = file($path . '/' . $filename);
    foreach ($file_content as $line) {
        foreach($contents_list as $content) {
            if(strpos($line, $content) !== false) {
                echo $line . '\n';
            }
        }
    }
}

遞歸 glob 函數的功勞轉到http://proger.i-forge.net/3_ways_to_recursively_list_all_files_in_a_directory/Opc

總而言之,在性能方面,您有以下排名(對於包含約 1200 個文件的非常大的目錄,使用兩種常見的文本模式,以秒為單位的結果):

  1. 通過 exec() 調用 grep - 2.2015s
  2. 使用遞歸glob並使用file()讀取文件 - 9.4443s
  3. 使用RecursiveDirectoryIterator並使用readline()讀取文件 - 15.1183s

即使在 2013 年,也有一種 - 在我看來更具可讀性 - PHP 原生方式來遞歸遍歷目錄樹: RecursiveDirectoryIterator類。

看看這個示例:

<?php

  // Initialize Recursive Iterator

  $directory = new RecursiveDirectoryIterator( 'path/to/project/' );
  $iterator = new RecursiveIteratorIterator( $directory );
  $regex = new RegexIterator( $iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH );

  // Iterate over files

  $files = array();
  foreach ( $regex as $info ) {
    // Do something with file to be found at $info->getPathname()
  }

?>

來自薩爾茨堡的問候!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM