Forum Moderators: coopster
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
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 -->
$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);
list ($sec1, $sec2, $sec3) = split ('\¦', $contents);
Next you can do what you want with each section and simply combine them again when ready:
$join = '
¦
';
$newcontents = $sec1.$join.$sec2.$join.$sec3;
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!';}
Hope this helps!
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?