Jump to content

[SOLVED] Split the contents of the text file to multiple text files


joshblue

Recommended Posts

<?php

$filename = "data.txt";

$file = file($filename);
$i = 1;
$current = array();
foreach($file as $line)
{
if(preg_match("|^\s*$|", $line) && !empty($current))
{
	file_put_contents($i.".txt", implode("\n", $current));
	$current = array();
	$i++;
}
else
	$current[] = $line;
}

if(!empty($current))
file_put_contents($i.".txt", implode("\n", $current));

echo "File split into {$i} files successfully";

?>

 

 

Orio.

<?php

$filename = "data.txt";

$file = file($filename);
$i = 1;
$current = array();
foreach($file as $line)
{
if(preg_match("|^\s*$|", $line) && !empty($current))
{
	file_put_contents($i.".txt", implode("\n", $current));
	$current = array();
	$i++;
}
else
	$current[] = $line;
}

if(!empty($current))
file_put_contents($i.".txt", implode("\n", $current));

echo "File split into {$i} files successfully";

?>

 

 

Orio.

 

It worked!

 

Thank very much!  ;D ;D ;D

 

 

<?php

$filename = "data.txt"; //The data you want to split

$file = file($filename); //Read the file into an array- each line as a element
$i = 1; //Counter
$current = array(); //Holds the lines from the previous empty-line (or file start) to the next empty-line (or file end)
foreach($file as $line) //Go over each line
{
if(preg_match("|^\s*$|", $line) && !empty($current)) //If an empty line was found (contains only whitespaces) AND $current is not empty (we are not facing a two empty lines in a row)
{
	file_put_contents($i.".txt", implode("\n", $current)); //Put the data in current into a file (see implode and file_put_contents in the manual
	//Reset the data ($current) and advance the counter
	$current = array();
	$i++;
}
else
	$current[] = $line; //Add data (the current non empty line) to $current
}

if(!empty($current)) //Add the last chunk of data to a file
file_put_contents($i.".txt", implode("\n", $current));

echo "File split into {$i} files successfully"; //Output...

?>

 

Enjoy.

 

Orio.

<?php

$filename = "data.txt"; //The data you want to split

$file = file($filename); //Read the file into an array- each line as a element
$i = 1; //Counter
$current = array(); //Holds the lines from the previous empty-line (or file start) to the next empty-line (or file end)
foreach($file as $line) //Go over each line
{
if(preg_match("|^\s*$|", $line) && !empty($current)) //If an empty line was found (contains only whitespaces) AND $current is not empty (we are not facing a two empty lines in a row)
{
	file_put_contents($i.".txt", implode("\n", $current)); //Put the data in current into a file (see implode and file_put_contents in the manual
	//Reset the data ($current) and advance the counter
	$current = array();
	$i++;
}
else
	$current[] = $line; //Add data (the current non empty line) to $current
}

if(!empty($current)) //Add the last chunk of data to a file
file_put_contents($i.".txt", implode("\n", $current));

echo "File split into {$i} files successfully"; //Output...

?>

 

Enjoy.

 

Orio.

 

THANK YOU VERY MUCH!  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.