3

PHPを使用して、大きなテキストファイルを文字数ごとに個別のファイルに分割するにはどうすればよいですか? したがって、10,000 文字ごとに分割された 10,000 文字のファイルは、10 個のファイルに分割されます。また、ピリオドが見つかった後にのみ分割できますか?

ありがとう。

更新 1:私は zombats コードが好きで、いくつかのエラーを削除して次のように思いつきましたが、完全停止後にのみ分割する方法を知っている人はいますか?

$i = 1;
    $fp = fopen("test.txt", "r");
    while(! feof($fp)) {
        $contents = fread($fp,1000);
        file_put_contents('new_file_'.$i.'.txt', $contents);
        $i++;
    }

更新 2: zombats の提案を受けて、コードを以下のように変更したところ、動作するようです -

$i = 1;
    $fp = fopen("test.txt", "r");
    while(! feof($fp)) {
        $contents = fread($fp,20000);
        $contents .= stream_get_line($fp,1000,".");
        $contents .=".";

        file_put_contents("Split/".$tname."/"."new_file_".$i.".txt", $contents);
        $i++;
    }
4

6 に答える 6

5

これは、基本的なfread()を使用して簡単に実行できるはずです。読み取るバイト数を指定できるため、正確な量を読み取って新しいファイルに出力するのは簡単です。

次のようなものを試してください。

$i = 1;
$fp = fopen("test.txt",'r');
while(! feof($fp)) {
    $contents = fread($fp,1000);
    file_put_contents('new_file_'.$i.'.txt',$contents);
    $i++;
}

編集

特定の長さの後で、または特定の文字で停止したい場合は、の代わりにstream_get_line()fread()を使用できます。必要な終了区切り文字を指定できることを除いて、ほとんど同じです。読み取りの一部としてデリメータを返さないことに注意してください。

$contents = stream_get_line($fp,1000,".");
于 2010-01-11T19:41:56.063 に答える
4

run 関数にバグがあります。変数$splitが定義されていません。

于 2012-11-20T11:45:42.197 に答える
1

これを行うためのクラスを作成することもできます。

<?php

/**
* filesplit class : Split big text files in multiple files
*
* @package
* @author Ben Yacoub Hatem <hatem@php.net>
* @copyright Copyright (c) 2004
* @version $Id$ - 29/05/2004 09:02:10 - filesplit.class.php
* @access public
**/
class filesplit{
    /**
     * Constructor
     * @access protected
     */
    function filesplit(){

    }

    /**
     * File to split
     * @access private
     * @var string
     **/
    var $_source = 'logs.txt';

    /**
     *
     * @access public
     * @return string
     **/
    function Getsource(){
        return $this->_source;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setsource($newValue){
        $this->_source = $newValue;
    }

    /**
     * how much lines per file
     * @access private
     * @var integer
     **/
    var $_lines = 1000;

    /**
     *
     * @access public
     * @return integer
     **/
    function Getlines(){
        return $this->_lines;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setlines($newValue){
        $this->_lines = $newValue;
    }

    /**
     * Folder to create splitted files with trail slash at end
     * @access private
     * @var string
     **/
    var $_path = 'logs/';

    /**
     *
     * @access public
     * @return string
     **/
    function Getpath(){
        return $this->_path;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setpath($newValue){
        $this->_path = $newValue;
    }

    /**
     * Configure the class
     * @access public
     * @return void
     **/
    function configure($source = "",$path = "",$lines = ""){
        if ($source != "") {
            $this->Setsource($source);
        }
        if ($path!="") {
            $this->Setpath($path);
        }
        if ($lines!="") {
            $this->Setlines($lines);
        }
    }


    /**
     *
     * @access public
     * @return void
     **/
    function run(){
        $i=0;
        $j=1;
        $date = date("m-d-y");
        unset($buffer);

        $handle = @fopen ($this->Getsource(), "r");
        while (!feof ($handle)) {
          $buffer .= @fgets($handle, 4096);
          $i++;
              if ($i >= $split) {
              $fname = $this->Getpath()."part.$date.$j.txt";
               if (!$fhandle = @fopen($fname, 'w')) {
                    print "Cannot open file ($fname)";
                    exit;
               }

               if (!@fwrite($fhandle, $buffer)) {
                   print "Cannot write to file ($fname)";
                   exit;
               }
               fclose($fhandle);
               $j++;
               unset($buffer,$i);
                }
        }
        fclose ($handle);
    }


}
?>


Usage Example
<?php
/**
* Sample usage of the filesplit class
*
* @package filesplit
* @author Ben Yacoub Hatem <hatem@php.net>
* @copyright Copyright (c) 2004
* @version $Id$ - 29/05/2004 09:14:06 - usage.php
* @access public
**/

require_once("filesplit.class.php");

$s = new filesplit;

/*
$s->Setsource("logs.txt");
$s->Setpath("logs/");
$s->Setlines(100); //number of lines that each new file will have after the split.
*/

$s->configure("logs.txt", "logs/", 2000);
$s->run();
?>

ソース http://www.weberdev.com/get_example-3894.html

于 2010-01-11T19:41:42.617 に答える
1

最も簡単な方法は、ファイルの内容を読み取り、内容を分割してから、別の 2 つのファイルに保存することです。ファイルが数ギガバイトを超える場合、整数サイズの制限により、PHP でそれを実行すると問題が発生します。

于 2010-01-11T19:21:25.120 に答える