【问题标题】:PHP - How do I open files and read them then write new ones with "x" lines per file?PHP - 如何打开文件并读取它们,然后用每个文件的“x”行编写新文件?
【发布时间】:2010-09-11 14:28:50
【问题描述】:

我之前在这里发布过这个问题,但没有任何回应。我可能做错了什么,这里再次提供更多细节。

目录中的文件命名为 1.txt、2.txt、3.txt 等。下面的 sn-p 进入该目录,打开所有读取它们的 *,txt 文件,删除欺骗并创建一个包含所有独特内容的文件。 (本例中的名称)。

$files = glob($dirname."/*.txt"); //matches all text files
    $lines = array();
    foreach($files as $file)
    {
    $lines = array_merge($lines, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
    }
    $lines = array_unique($lines);
    file_put_contents($dirname."/allofthem.txt", implode("\n", $lines));
    }

以上内容对我来说非常有用!感谢 stackoverflow 的大力帮助。

但是,我想更进一步。

如何修改上面的代码,而不是一个大的重复免费“allofthem.txt”文件,以从新数据中创建每个最多包含 5oo 行的文件?

他们需要进入一个新目录,例如 $dirname."/done/".$i.".txt" 我已经尝试在循环中计数,但我的努力没有奏效,最终变成了一英里长。

我还尝试将 500 推入一个数组,递增到另一个数组并以这种方式保存。没运气。我只是没有“得到”它。

同样,这个初学者需要一些专家的帮助。提前致谢。

【问题讨论】:

    标签: php glob


    【解决方案1】:

    根据您的代码获得行数组后,您可以使用 array_chunk 将其分成 500 行的块,然后将每个块写入自己的文件:

    // ... from your code
    $lines = array_unique($lines);
    
    $counter = 1;
    foreach (array_chunk($lines, 500) as $chunk)
    {
      file_put_contents($dirname . "/done/" . $counter . ".txt", implode("\n", $chunk));
      $counter++;
    }
    

    【讨论】:

      【解决方案2】:

      这个功能会带你到某个地方!

      function files_identical($fn1, $fn2) {
          if(filetype($fn1) !== filetype($fn2))
              return FALSE;
      
          if(filesize($fn1) !== filesize($fn2))
              return FALSE;
      
          if(!$fp1 = fopen($fn1, 'rb'))
              return FALSE;
      
          if(!$fp2 = fopen($fn2, 'rb')) {
              fclose($fp1);
              return FALSE;
          }
      
          $same = TRUE;
          while (!feof($fp1) and !feof($fp2))
              if(fread($fp1, 4096) !== fread($fp2, 4096)) {
                  $same = FALSE;
                  break;
              }
      
          if(feof($fp1) !== feof($fp2))
              $same = FALSE;
      
          fclose($fp1);
          fclose($fp2);
      
          return $same;
      }
      

      源:http://www.php.net/manual/en/function.md5-file.php#94494

      【讨论】:

        【解决方案3】:
        $files = glob($dirname."/*.txt"); //matches all text files
        $lines = array();
        foreach($files as $file)
        {
           $lines = array_merge($lines, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
        }
        $lines = array_unique($lines);
        $lines_per_file = 500;
        $files = count($lines)/$lines_per_file;
        if(count($lines) % $lines_per_file > 0) $files++;
        for($i = 0; $i < $files; $i++) {
            $write = array_slice($lines, $lines_per_file * $i, $lines_per_file);
            file_put_contents($dirname."/done/".$i.".txt", implode("\n", $write));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-05
          • 1970-01-01
          • 1970-01-01
          • 2022-01-14
          • 2014-04-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多