Jump to content

split the text from a .txt file between blank lines into multiple .txt files


maxmjessop

Recommended Posts

Hi,  I have a .txt file with blocks of text separated by blank lines. I need to save each block into separate .txt files on the server.

I am a complete novice and so far only managed to load the .txt and echo it back. I've spent two days searching for examples but have come up blank, I think explode() into an array and then write the arrays to txt files is the way to go. Any help would be appreciated thanks.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

try

<?php
$fn = 'fpdf multirow code.txt';
$fcount = $lcount = 0;
$fin = fopen($fn, 'r');
while (!feof($fin))
{
    $line = fgets($fin, 1024);
    if (trim($line))
    {
        $lcount++;
        if ($lcount==1) $fout = fopen('myfile'.++$fcount.'txt', 'w');
        fwrite($fout, $line);
    }
    else
    {
        if ($lcount) fclose ($fout);
        $lcount = 0;
    }
}
if ($lcount) fclose ($fout);
fclose ($fin);
?>

Link to comment
Share on other sites

Hey and welcome to phpfreaks :)

 

Depending on the structure of your text file and the platform on which it was created you have several options.

 

First of all you will read the entire file into a string like this:

 

<?php

$file = 'some/place/txtfile.txt';

$content = file_get_contents($file);

?>

 

Next you have to choose your "solution" among the following two cases:

 

#1: All the text blocks in your file is separated by precisely the same number of blank lines

 

#2: The text blocks are separated by a variable number of blank lines

 

In case #1 you can use explode() like this:

 

<?php

// Use "\n" or "\r\n" as separator depending on the text editor and platform used to create your text file.

// Add the separator n+1 times, where n is the number of blank lines between your blocks. The below example is for 1 blank line separating the blocks (hence \n two times)
$array = explode("\n\n", $content);

echo '<pre>';
print_r($array);
echo '</pre>';

?>

 

In case #2 you can use preg_split() like this:

 

<?php

// use "/\n{2,}/" or "/(\r\n){2,}/" depending on text editor and platform. Alternatively use "/(\n|\r\n){2,}/" which accepts both \n and \r\n as line break.

$array = preg_split("/\n{2,}/",$content);

echo '<pre>';
print_r($array);
echo '</pre>';

?>

 

I've successfully used both methods on the following test text file:

 

Note: A Note on Line Feeds
Line feeds have little meaning in HTML, however it is still a good 
idea to make your HTML look nice and clean by putting line feeds in. 
A linefeed that follows immediately after a closing ?> will be removed 
by PHP. This can be extremely useful when you are putting in many 
blocks of PHP or include files containing PHP that aren't supposed 
to output anything. At the same time it can be a bit confusing. You 
can put a space after the closing ?> to force a space and a line 
feed to be output, or you can put an explicit line feed in the last 
echo/print from within your PHP block.

Note: A Note on Text Editors
There are many text editors and Integrated Development Environments (IDEs) 
that you can use to create, edit and manage PHP files. A partial list of 
these tools is maintained at » PHP Editors List. If you wish to recommend 
an editor, please visit the above page and ask the page maintainer to add 
the editor to the list. Having an editor with syntax highlighting can be 
helpful.

Note: A Note on Word Processors
Word processors such as StarOffice Writer, Microsoft Word and Abiword 
are not optimal for editing PHP files. If you wish to use one for this 
test script, you must ensure that you save the file as plain text or 
PHP will not be able to read and execute the script.

Note: A Note on Windows Notepad
If you are writing your PHP scripts using Windows Notepad, 
you will need to ensure that your files are saved with the 
.php extension. (Notepad adds a .txt extension to files 
automatically unless you take one of the following steps
to prevent it.) When you save the file and are prompted 
to provide a name for the file, place the filename in 
quotes (i.e. "hello.php"). Alternatively, you can click on 
the 'Text Documents' drop-down menu in the 'Save' dialog 
box and change the setting to "All Files". You can then 
enter your filename without quotes. 

 

which is taken from php.net's tutorial on php.

 

Edit: Then of course use fwrite() to write each element form the array (which equals a text block) to another file.

 

Hope this helps!

Link to comment
Share on other sites

Barand, may I ask why you didn't do some form of split / explode and "parsed" the file line by line?

 

Not that it isn't smart, I actually haven't thought of testing for a blank line by if(trim(line)) - I was immediately thinking regex and ^$ to match a blank line... which I didn't got working.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.