By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,615 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,615 IT Pros & Developers. It's quick & easy.

split content of a text file and export to new txt files

P: 3
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.
Oct 22 '08 #1
Share this Question
Share on Google+
4 Replies


code green
Expert 100+
P: 1,726
The php file() function will load each line of a file into an array.
That is a line ending with CR / LF (system dependent).
Loop through the array concatenating each element onto a string variable until you find an empty element.
You have block 1. Write it to a file
Then start another string variable.and continue looping through the array.

I could write example code but this is very easy.
And PHP file functions are simply excellent.

Well they are after years of writng C++ FIFO streams
Oct 22 '08 #2

P: 3
The php file() function will load each line of a file into an array.
That is a line ending with CR / LF (system dependent).
Loop through the array concatenating each element onto a string variable until you find an empty element.
You have block 1. Write it to a file
Then start another string variable.and continue looping through the array.

I could write example code but this is very easy.
And PHP file functions are simply excellent.

Well they are after years of writng C++ FIFO streams
Well that does sound a lot simpler than all the pages of code i've looked.
I'll go and have a look at some file() tutorials now but if you have the time or energy I would be greatful of an example code?? or a hint.

Thank you
Oct 22 '08 #3

P: 3
it maybe I've spent too long looking at code the last few days, I'm confused..
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file=file("menu4.txt", "r") or die('Could not read file!');
  3. ?>
this is as far as i've got and now i'm not sure if how to set up the loop. If you could right me an example that would be great and save my head from melting.

Thanks
Oct 22 '08 #4

P: 3
Here we're going to use one of my favorite functions for this sort of thing: preg_replace. Now please not that the first field is an input for regular expressions, so be sure to keep the "/ and /" intact! Whether you enter \n\n or \r\r, it should look like "/\n\n/" or "/\r\r/".

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $sourceFile = "fileWeWillRead.txt";
  4. $targetFile = "fileWeWillWrite.txt";
  5.  
  6. $sourceFilehandle = fopen($sourceFile, "r"); // Read the file
  7. $sourceFileContents = fread($sourceFilehandle, filesize($sourceFile)); // Store information to variable
  8. fclose($sourceFilehandle); // Close file
  9.  
  10. // If a unix-based program created this file, you'll probably have to enter \r\r
  11. // If a windows-based program created this file, you'll probably have to enter \n\n
  12. // There is also a chance you'll have to enter in \n\r\n\r or \r\nrl\n too!
  13. // If one doesn't work, try another!! :)
  14.  
  15. $targetFileContents = preg_replace("/\n\n/", "|", $sourceFileContents);
  16.  
  17. $targetFileHandle = fopen($targetFile, 'w'); // Open file or prepare to create it
  18. fwrite($targetFileHandle, $targetFileContents); // Write to file
  19. fclose($targetFileHandle); // Close file
  20.  
  21. print $targetFileContents;
  22.  
  23. ?>
Using this code: Let's test the following:

Your sourceFile looks like this:
Expand|Select|Wrap|Line Numbers
  1. this is line 1
  2.  
  3. this is line 2
  4.  
  5. this is yet another line
After parsing that file through your PHP script, your targetFile should contain the following:
Expand|Select|Wrap|Line Numbers
  1. this is line 1|this is line 2|this is yet another line
I hope this helps you out! Any other questions, just contact me. :)
Oct 23 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.