Page 1 of 1
update text file
Posted: Thu Aug 24, 2006 1:00 pm
by duk
hy
if i read a file with 500 lines, and if i want to replace text at the line 200 how can i do this ??? using fwrite()
thanks in advance
Posted: Thu Aug 24, 2006 3:12 pm
by SomeOne
Code: Select all
function replace_my_line($line, $new)
{
$line = 200; //if you define number of line with function call comment this line!
$file = "yourfile.txt";
$a = file("$file"); //first load txt file inty array
$new // holds your new text for line 200
$a[$line] = $new //replace the line 200 with your new content
$a = implode("", $a) //make a writable string (you cannot write array into txt file)
$handleTXT = fopen($file, "w");//open TXT file
fwrite($handleTXT, $a); //write the content to txt file
fclose($handleTXT); // close txt file...
}
you can call this function by;
Code: Select all
replace_my_line($line, $new);
//$new is NEW content of a line
//$line is number of line in txt file
i write it on the fly so sorry if there are some bugs..
this should work
Posted: Sat Aug 26, 2006 8:33 am
by duk
humm the ideia is put the file in the array and then write everything into it again...
the only problem i see is if anything make the cpu stop i will loose all information untill the line he wrote...
but anyway nice, well spoted

Posted: Sat Aug 26, 2006 11:54 am
by Ollie Saunders
duk wrote:the only problem i see is if anything make the cpu stop i will loose all information untill the line he wrote...
Is that likely to happen? If it does is it your problem?
Posted: Sat Aug 26, 2006 1:01 pm
by RobertGonzalez