Forum Moderators: coopster

Message Too Old, No Replies

Parse/Manage Flat File in Sections

How can i parse/edit/update a flat file in sections

         

cyberd

10:18 am on Dec 15, 2004 (gmt 0)

10+ Year Member



Hello everyone,

First of all i would like to say that i find the forum very useful and so far it has helped me in several cases, as i'm rather new to php...

I have an issue with a flat file i'm trying to parse/manage...
It's actually a template file, but it has gotter to big to manage online and i want to somehow manage editing and updating it in sections.
The template file is of the form...


<!-- BEGIN: Section1 -->
HTML content of Section 1
<!-- END: Section1 -->

<!-- BEGIN: Section2 -->
HTML content of Section 2
<!-- END: Section2 -->

<!-- BEGIN: Section3 -->
HTML content of Section 2
<!-- END: Section3 -->

So far, with help from the forum, i have managed to load the whole file and edit and update and save it again.
What i want to do now is load each section of the file separately and then be able to edit/update it and save the whole file back again updated....

Thinking in ASP now (i'm more comfortable) I think i need to load the whole file up to the occurence of '<!-- BEGIN: Section? -->' to one variable, then geting the section up until '<!-- END: Section? -->' to a second variable, to be displayed in a textbox for editing and finally the rest of the file to a third variable.
After editing in the second variable displayed in the textbox...all 3 variables are comined to save the whole file again...

Still i have trouble making this happen? How do i parse to find a specific identifier within the file?
And would the above variables combined be giving me the whole file or would i end up with a corrupt file?

All help would be very much appreciated!
Thank you in Advance

Hester

12:21 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your method sounds to be what you need. However I would simply split the file into 3. Or use 2 sets of files - 1, with the 3 sections, and 1 which is combined.

To do it the way you suggest, simply read the whole file into a variable. Then break it down - perhaps the best way would be to introduce a single character between each section that you can use to split the variable into 3. (Perhaps the section comments aren't needed?) Eg:


<!-- BEGIN: Section1 -->
HTML content of Section 1
<!-- END: Section1 -->
¦
<!-- BEGIN: Section2 -->
HTML content of Section 2
<!-- END: Section2 -->
¦
<!-- BEGIN: Section3 -->
HTML content of Section 2
<!-- END: Section3 -->

There, I've added the pipe symbol. If this is used in the sections you'll need to parse them first and replace any pipe symbols with another symbol. You can always replace it back later. Now you can do this:

$fn = 'filename.txt'; // your database
$fpc = fopen ($fn, 'r+'); // read/write
if (!$fpc) {echo 'ERROR: Unable to open remote file!'; exit;}
$contents = fread($fpc, filesize ($fn));
fclose ($fpc);

So now the whole file is in a variable $contents. Time to split it by the pipe symbol to give you the 3 sections:

list ($sec1, $sec2, $sec3) = split ('\¦', $contents);

Note: The symbol needs escaping with a slash.

Next you can do what you want with each section and simply combine them again when ready:


$join = '
¦
';
$newcontents = $sec1.$join.$sec2.$join.$sec3;

What that does is firstly define the pipe symbol along with 2 line breaks either side - keep these in the code exactly as it's shown. Then each section is added together with the pipes to make a variable containing everything ready to replace the old file.

Note: $fn was defined as the filename above. You may also need to make your file writable. The standard method if you are on an Apache server is to change the properties of the file once uploaded using an FTP program and the CHMOD command. (In WS_FTP you right-click on a highlighted file and select CHMOD, then tick the 3 Write boxes. You may need to tick some or all of the Execute ones too if it doesn't let you alter the file in PHP later. Other programs let you enter a number that relates to the tick boxes.)


if (is_writable($fn)) {
$fpc2 = fopen ($filename, 'wb');
// Writing only; place file pointer at start of file and truncate to zero length
fwrite ($fpc2, $newcontents);
fclose ($fpc2);
} else {echo 'ERROR: The file cannot be written to!';}

Note how the file is accessed using 'wb' instead of 'r+'. This could cause a problem - if the computer crashes at this point, or something else happens, the file is effectively destroyed! (It is made zero length.) Maybe there's a way to write a temporary file first then overwrite the new one.

Hope this helps!

cyberd

1:19 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



Thank you for your response and all the information, but I'm not sure that this going to work....
I understand exactly what you are saying but the problem is that since this is a template file...it is actually structured as following:
<!-- BEGIN:main -->
HTML...
.........
<!-- BEGIN: Section1 -->
HTML content of Section 1
<!-- END: Section1 -->
<!-- BEGIN: Section2 -->
HTML content of Section 2
<!-- END: Section2 -->
<!-- BEGIN: Section3 -->
HTML content of Section 3
<!-- END: Section3 -->
........
....HTML
<!--END:main -->

So adding the ¦ symbol in between the sectiond, would mean that this is presented in the html code and be displayed in the pages where the template is used....
How could i overcome this?