PHP 和正则表达式在目录中查找文件然后运行正则表达式

发布于 2024-08-09 17:24:35 字数 309 浏览 0 评论 0原文

在不同目录中查找文本文件然后对这些找到的文件运行正则表达式的最有效方法是什么。我将在本地运行 php/scripts。

假设我有 D:\Script\ 我想从中运行脚本,还有 D:\Script\folder_01、D:\Script\folder_02 等,我希望该脚本从中查看文件。这些文件夹名称不符合逻辑,它们可以是任何名称。

因此,我不想找到每个文件,而只想找到包含以下行的文件:“游戏类型:xxxxx”。 (匹配的文本文件数量约为 150-200),

找到这些文件后,我想一次运行一些 preg_replace 一个文件。

谢谢。

what's the the most efficient way to find text files in different directories and then run regex to those found files. I will run php/scripts locally.

Let's say I have D:\Script\ where i want to run my script from and D:\Script\folder_01, D:\Script\folder_02, etc. where i want that script to look the files from. Those folder names aren't logical, they could be anything.

So, i don't want to find every files but only the files that contains the line: "Game type: xxxxx". (matching number of text files would be around 150-200)

And after finding those files, I'd like to run some preg_replace one file at a time.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-08-16 17:24:35

如果您要在本地运行此程序,其他语言可能会更快或更容易实现。

不过,如果您想使用 PHP 来实现此目的:

  1. 递归地列出某个目录向下的所有文件的列表。所以这可能是 D:\scripts\foo 和 D:\scripts\foo\bar 等...(使用递归函数,scandir() 等)。
  2. 每个文件保存您找到该文件的完整路径和文件名。
  3. 现在遍历数组,对于找到的每个文件调用一个打开该文件的函数,在每一行上对所需的字符串执行正则表达式,如果找到该字符串则返回 TRUE。如果函数返回 TRUE,则将路径+文件名保存在第二个数组中。您现在拥有包含所需字符串的所有文件的列表。
  4. 遍历您创建的第二个数组,每个文件调用一个打开文件的函数,读取每一行并执行 preg_replace()。

如果您不需要找到某个字符串的所有文件的列表,则可以将步骤 3 和 4 合并为一个。

在代码中:

$fileList = giveFileList('D:\scripts\foo');
$selectedFileList = array();
for( $i = 0 ; $i < count($fileList) ; $i++ )
{
    if(stringExistsInFile($fileList[$i]))
    {
        $selectedFileList[] = $fileList[$i];    
    }
}

for( $i = 0 ; $i < count($selectedFileList) ; $i++ )
{
    replaceStringInFile($selectedFileList[$i]);
}

If you're going to run this locally, other languages are probably faster and or easier to implement.

Nevertheless, if you want to use PHP for this:

  1. Recursively make a list of all the files from a certain directory downwards. So that could be D:\scripts\foo and D:\scripts\foo\bar etc... (use a recursive function, scandir() etc).
  2. Per file save the complete path in which you found the file and the file name.
  3. Now walk through the array and for every file you find call a function that opens that file, does a regex on every line for the desired string and return TRUE if that string is found. If the function returns TRUE, save the path+filename in a second array. You now have a list of all the files that contain the desired string.
  4. Walk through the second array you made, per file call a function that opens the file, reads every line and does a preg_replace().

Steps 3 and 4 can be folded into one if you don't need a list of all the files where you find a certain string.

In code:

$fileList = giveFileList('D:\scripts\foo');
$selectedFileList = array();
for( $i = 0 ; $i < count($fileList) ; $i++ )
{
    if(stringExistsInFile($fileList[$i]))
    {
        $selectedFileList[] = $fileList[$i];    
    }
}

for( $i = 0 ; $i < count($selectedFileList) ; $i++ )
{
    replaceStringInFile($selectedFileList[$i]);
}
许一世地老天荒 2024-08-16 17:24:35

这能完成全部任务吗?或者我错过了一些重要的事情?
抱歉我是新手! :)

if ($handle = opendir('/scripts')) {
    while (false !== ($filename = readdir($handle))) {
       $openFile = fopen($filename, "r")  or die("Can't open file");
       $file = fread($openFile, filesize($filename));
       $pattern = '/^Game Type: xxx$/im';
       if (preg_match($pattern, $file)) {
         // preg_replace goes here
         // fopen newFile goes here
         // fwrite newFile goes here
         // fclose newFile goes here
       }
       fclose($openFile);
    }

    closedir($handle);
} 

Will this do the whole trick? Or am I missing something crucial?
Sorry i'm a newbie! :)

if ($handle = opendir('/scripts')) {
    while (false !== ($filename = readdir($handle))) {
       $openFile = fopen($filename, "r")  or die("Can't open file");
       $file = fread($openFile, filesize($filename));
       $pattern = '/^Game Type: xxx$/im';
       if (preg_match($pattern, $file)) {
         // preg_replace goes here
         // fopen newFile goes here
         // fwrite newFile goes here
         // fclose newFile goes here
       }
       fclose($openFile);
    }

    closedir($handle);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文