only 17? cool. I'm only 16 and I started PHP ever since version 2.0 so I kinda know most of it :-)
Anyway, there are two ways you can do this (as far as I know). There are probably better ways, but here are my suggestions:
Method 1:
1) read the contents of the file you want to an array. call it ... $contents
$contents = file("myfile.txt");
2) start a newfile (or overwrite the old one)
$newfile = fopen("myfile.txt", "w");
3) run through each line of contents and if it is the line you want to change, write the new line to the $newfile handle. Else, just write back the original line:
for($i = 0; $i < sizeof($contents); $i++) {
if(eregi("hello", $contents[$i])) {
// line matched
// write it to the newfile handle. make sure you put in \n for newline!
fwrite($newfile, "this is a new line\n");
}
else {
fwrite($newfile, $contents[$i]);
fwrite($newfile, "\n");
}
}
in the above example, I'm searching for the word "hello" in the line. If it matches, i want to write the line "this is a new line" instead of the old line.
That's all. If you want to delete a line, inside the if(eregi....) statement block, just put nothing. So it will not insert that line.
Method 2:
it will take too long to explain here. But if you know the exact position where you want to add a line, you can use fopen("myfile.txt", "r") and then use fseek to goto the correct place and start adding. This method is more tedious so I suggest method one.
let me know if this helps. if you want, I can write a sample program that will demonstrate adding/changing/deleting lines.
-sridhar