在PHP中拆分文本文件

时间:2010-01-11 19:18:11

标签: php string text-processing

如何使用PHP通过字符计数将大文本文件拆分为单独的文件?因此,每1000个字符拆分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++;
    }

6 个答案:

答案 0 :(得分:4)

运行功能有一个错误;变量$split未定义。

答案 1 :(得分:3)

您应该可以使用基本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,".");

答案 2 :(得分:1)

最简单的方法是读取文件内容,拆分内容,然后保存到另外两个文件。如果你的文件超过几千兆字节,由于整数大小的限制,你将在PHP中遇到问题。

答案 3 :(得分: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

答案 4 :(得分:1)

我已经修复了课程,并且完善了.txt文件。

<?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(){

        $buffer = '';
        $i=0;
        $j=1;
        $date = date("m-d-y");
        $handle = @fopen ($this->Getsource(), "r");

        while (!feof ($handle)) {

            $buffer .= @fgets($handle, 4096);
            $i++;

            if ($i >= $this->getLines()) { 

                // set your filename pattern here.
                $fname = $this->Getpath()."split_{$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);
            }
        } 
        if ( !empty($buffer) && !empty($i) ) {
            $fname = $this->Getpath()."split_{$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);
            unset($buffer,$i);  
        }
        fclose ($handle);
    }


}
?>

用法示例

<?php

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();
?>

答案 5 :(得分:0)

在运行功能中,我进行了以下调整,以修复&#34;拆分未定义&#34;警告。

function run(){

        $buffer='';
        $i=0;
        $j=1;
        $date = date("m-d-y");
        $handle = @fopen ($this->Getsource(), "r");

        while (!feof ($handle)) {

            $buffer .= @fgets($handle, 4096);
            $i++;

            if ($i >= $this->getLines()) { // $split was here, empty value..

                // set your filename pattern here.
                $fname = $this->Getpath()."dma_map_$date.$j.csv";

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

我使用此类将500,000行CSV文件拆分为10个文件,因此phpmyadmin可以使用它而不会出现超时问题。工作就像一个魅力。