473,750 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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
4 2475
code green
1,726 Recognized Expert Top Contributor
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
maxjessop
3 New Member
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
maxjessop
3 New Member
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
JDBurnZ
3 New Member
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

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

Similar topics

5
3091
by: ted | last post by:
Hello, I have a HTML table in my XSL that basically handles the layout of the whole page. I'm using xsl:include to split up the XSL into separate files. Is there a trick or technique to put part of a HTML table (without closing a td or tr tag) in one of those separate files? Right now I need to close every tag or the transformation will fail. For instance, I can't have "<table><tr><td>some stuff" in one file and
1
10862
by: TJ | last post by:
I am very new to C# (this is my first real project), therefore please be patient if my question is considered being to newbie. I am modifying a program which takes a text file, does some formatting then outputs the resulting file. What I am trying to do is have the program make as many files as is needed, each limited to a given file size (in this case 240KB).
3
8572
by: Krish | last post by:
I have requirement, that i get one big chunk of text file. This text file will have has information, that on finding "****End of Information****", i have to split them individual text file with our naming standard (unique id) and create them designated folder. This requirement should be created as a batch job and preferrably this job should monitor the folder where one big chunk of text file lands and process them immediately. ...
5
2185
by: Alan Searle | last post by:
I am exporting ms-access data to XML files. This works fine. However, I need to insert one line at the top of each exported file (i.e. a reference to the XSL file) and am having a problem with this. First I did the following: Export the XML file (using standard XML export) and then read the file (line by line) and writing it out (together with the required extra line) to a new file.
2
1819
by: Steve Cartnal | last post by:
I need to copy records from several different tables into one text file. Although the tables have many common fields, they also contain several that are different from table to table. I can copy and paste the dissimilar records manually into the text file, so it would seem that should be possible to do programatically. I set up export specifications for each of the twelve tables and tried to export each to the same text file. This...
5
3440
by: JHNielson | last post by:
I have a somewhat simple question, but have been baffled by it for a while, and now I'm on a tight deadline - have to get it done within 24 hours. I am trying to export a set of files to my hard drive to then later be FTPd to a server. the file format should be "TITLE.yyyymmdd" where yymmdd is the date code for when the files are created. The files can't be .txt. But when I run the files as ".txt" the code runs just fine. when I...
4
12557
by: chimambo | last post by:
I have 2 problems: 1. I want to import a single text file into an access table using a Macro. I am however getting an error that I need to put a specification name argument. What does this mean? It also suggests that I add a schema.ini in the source folder. What does this .ini do and how do I create and use it? 2. After this is resolved, I would like to be able to import several text files using a macro into the same table.
16
7246
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle Fairfield to accomplish the first step: Private Sub SaveObjectsAsText() path = CurrentProject.path & "\ObjectsAsText\" SaveDataAccessPagesAsText SaveFormsAsText SaveReportsAsText
7
4384
by: John Smith | last post by:
Hi, I am very new to C# and NET framework. I am trying to hash (using MD5CryptoServiceProvider) a source that is split into several files. Now when the source is in one file I can produce the correct md5 hash. My issue is how can I reproduce the correct hash when the file is split into different files.
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.