@shimonAug 13.2004 — #Seems clear enough to me - neenach has a text file of a certain amount of lines - let's say 20 lines, and wants to insert some text between line 10 and line 11.
This is a long-winded way to do it but I can't think of a better way just now.
[code=php]<?php
// Let's say this is the text to insert $insert_text = 'Hi! I am some new text!';
// And we want to insert it between ln 10 and 11 $insert_after = 10;
// The filename $filenm = 'myfile.txt';
// Read in the contents of the text file // (you may wish to make a backup first) $contents = file_get_contents($filenm);
// Split contents into an array $contents = explode("n", $contents);
@shimonAug 13.2004 — #doh! well spotted ? In earlier PHP versions I got so used to having to work *around* the fact that file() returns an array, that I overlooked it when I needed it!
I also realised that file_put_contents() won't work before PHP5, but some combination of fopen(), fwrite() and fclose() will do the same job.
@neenach2002authorAug 13.2004 — #Now I want the line to be written after the last similar line. The line is similar to this: [CODE][link=Site domain]Site name[/link][/CODE]
If I can't do it this way, then I can always insert the data before the first line that looks like that, as that line number will always hold a similar value.
@neenach2002authorAug 13.2004 — #Alright....The code is now working...sort of... [code=php]// Let's say this is the text to insert $insert_text = '[link=' . $_POST['domain'] . ']' . $_POST['sname'] . '[/link]';
// And we want to insert it between ln 20 and 21 $insert_after = 20;
// The filename $filenm = '../e107_plugins/custom/Hosted.php';
// Read in the contents of the text file // (you may wish to make a backup first) $contents = file_get_contents($filenm);
// Split contents into an array $contents = explode("n", $contents);
// print the new contents back to file $new_contents = join('', $new_contents); $fpoint = fopen($filenm, 'w');
fwrite($fpoint , $new_contents); fclose($fpoint)[/code] My only problem is it doesn't write the file how I need it to. The file is rendered through the e107 CMS, which is why I need to write the previous contents back to the file, as well as keeping the 'Hosted sites' list intact. I want to add a site to the list every time someone applies for hosting.
The file that the above code spits out can be found [URL=http://hosting.kingdombattles.com/e107_plugins/custom/hosted1.txt]here[/URL]