I've seen many examples on this site about overwriting text files, but couldn't find a solution for this specific case. I am writing a little script using php and a txt file to store some data.
I have the following textile:
<div class="chatmsg"><b>John</b>: sfdfsdfd<br/></div>
<div class="chatmsg"><b>Jane</b>: dd<br/></div>
<div class="chatmsg"><b>John</b>: sdsd<br/></div>
<div class="chatmsg"><b>Jane</b>: sf<br/></div>
<div class="chatmsg"><b>Bob</b>: dsf<br/></div>
<div class="chatmsg"><b>Jane</b>: df<br/></div>
This is created using a php file which add a line:
if(!empty($name) && !empty($msg)){
$fp = fopen($chatroom."_chat.txt", 'a');
fwrite($fp, '<div class="chatmsg"><b>'.$name.'</b>: '.$msg.'<br/></div>'.PHP_EOL);
fclose($fp);
}
In a while, this file will grow huge. I could delete the file, but I want to try a different approach here.
Let's say 3 people added a line and the text file is like this:
<div class="chatmsg"><b>John</b>: sdsd<br/></div>
<div class="chatmsg"><b>Jane</b>: sf<br/></div>
<div class="chatmsg"><b>Bob</b>: dsf<br/></div>
Currently, new entries will add a line to the text file, but is it also possible to overwrite Bobs previous message with a new message? And if so, how to do this? This way I keep the text file small (so no more lines will be added unless a completely new person enters a line)
Thanks a lot