Solved

Breaking up a data file

Posted on 2009-05-05
3
250 Views
Last Modified: 2012-05-06
I am trying to write a php script that will read in a text based data file. Find the start of the first set of data and the End of the first set. Then make the changes that it needs to make, move the changed data to another var, and then go to the next set of data and then add append that data to the first set of data and repeat till the end of the file.

Below is an example of the data format. The data is separated by 3 blank lines. If i break the data into separate parts, I can do what I want.  But I need to read it as one file.

Any suggestions on the best way to do this?  Thanks!
startdata

data

data

data

data

enddata
 
 

startline

data

data

data

data

data

endline
 
 

startline

data

data

data

endline

Open in new window

0
Comment
Question by:cPokey
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
bljak earned 250 total points
ID: 24311881
Am i understanding you right?
You are having always chunks of data, which are separated by 3 (not 2 like in your example) empty lines.
Lets say your file looks like this:

------- start of file ----------
aaaaaaaaa
bbbbbbbbb
ccccccccc



ddddddddd
eeeeeeeee
fffffffffffff
gggggggg



hhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiii
------- end of file  ----------

Then, you want it to do something like this:

Open file, read data until end of first chunk has been reached, assign it to a variable, do something with that (maybe also assign changed stuff to another variable) and then move on.
And process would look like this:
You start doing it, then first time you reach 3 blank lines, you have variable called $tmp which looks liek this:
$tmp = "aaaaaaaaa
bbbbbbbbb
ccccccccc"

Then you might do something with that, whatever, and when you are done, you keep moving to the next chunk which then looks like this:
$tmp = "ddddddddd
eeeeeeeee
fffffffffffff
gggggggg"

Again, you do whatever you want with it, change it however you want it and move on again, and then you have reached end of file and have $tmp looking like this:

$tmp = "hhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiii"

If this is what you want, look down at the code, function "myFunc" is your function to change received chunk to whatever you want and assign it wherever you want.



<?php
 

$globalVar = "";
 

function myFunc($str)

{

	global $globalVar;

	$globalVar .= $str;

}
 

function SplitFileToChunks($filePath)

{

	$tmp = "";

	

	if ( ($fh = fopen($filePath, "r")) !== FALSE )

	{

		$endChunk = false;

		$emptyLineCnt = 0;

		

		while ( !feof($fh) )

		{

			$fileLine = fgets($fh);

			$tmpFileLine = trim($fileLine);

			

			if ( empty($tmpFileLine) )

			{

				$emptyLineCnt++;

			}

			

			$tmp .= $fileLine;

			

			if ( $emptyLineCnt == 3 )

			{

				$emptyLineCnt = 0;

				$tmp = trim($tmp); //this will also trim last CR/LF, if you want it, then uncomment next line

				//$tmp .= "\r\n";

				

				myFunc($tmp);

				

				$tmp = "";

			}

		}

		

		$tmp = trim($tmp);

		

		if ( !empty($tmp) )

		{

			myFunc($tmp);

		}

		

		fclose($fh);		

	}

}
 

SplitFileToChunks("<path to your file>");
 

var_dump($globalVar);
 

?>

Open in new window

0
 

Author Comment

by:cPokey
ID: 24315241
Works Great.  Thank you!
0
 

Author Closing Comment

by:cPokey
ID: 31578177
Works Great! Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php ssh2_scp_send 1 49
can windows 10 wamp send mail()? 3 34
html input type 3 22
using php variable inside javascript 5 14
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now