用php分割一个大的txt文件 [英] Split a big txt file with php

查看:341
本文介绍了用php分割一个大的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的txt文件(84mb),超过一百万行.

I have a big txt file (84mb) with over 1 million lines.

我想将其拆分为单独的50k行文件.我该怎么做?我在网上搜索,但未找到任何内容.

I want to split it in seperate 50k lines files. How can i do it? I searched on-line but I found nothing about it.

推荐答案

这是我的脚本的修改版本,位于:

This is a modified version of my script at : Read through huge text files and store each line in database

set_time_limit(0);

// Split by Byte
splitData("b.php", __DIR__ . "/test", 1024 * 50); //Split 50Kb

// Split By line
splitLine("b.php", __DIR__ . "/test", 50000);

函数

function splitData($filename, $destination, $chunkSize) {
    $pathInfo = pathinfo($filename);
    $handle = fopen($filename, 'rb');
    $counter = 0;
    if ($handle === false) {
        return false;
    }
    while ( ! feof($handle) ) {
        $counter ++;
        $filePart = $destination . DIRECTORY_SEPARATOR . $pathInfo['filename'] . "_" . $counter . "." . $pathInfo['extension'];
        touch($filePart);
        file_put_contents($filePart, fread($handle, $chunkSize));
    }
    $status = fclose($handle);
    return $status;
}

function splitLine($filename, $destination, $lineSize) {
    $pathInfo = pathinfo($filename);
    $handle = fopen($filename, 'rb');
    $counter = 0;
    $splitCount = 0;
    if ($handle === false) {
        return false;
    }

    $content = "";
    while ( ($buffer = fgets($handle, 4096)) !== false ) {
        $content .= $buffer;
        $counter ++;

        if ($counter >= $lineSize) {
            $splitCount ++;
            $filePart = $destination . DIRECTORY_SEPARATOR . $pathInfo['filename'] . "_" . $splitCount . "." . $pathInfo['extension'];
            touch($filePart);
            file_put_contents($filePart, $content);
            $content = "";
            $counter = 0;
        }
    }
    $status = fclose($handle);
    return $status;
}

这篇关于用php分割一个大的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆