-2

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

Maurice
  • 1,147
  • 2
  • 21
  • 49
  • 2
    "keep the log small" and saving a bunch of unnecessary HTML in it.. Why don't you just save names & messages and create the HTML on the fly? – kero Jan 07 '15 at 22:56
  • Because I read/load the log using SSE, so it needs to be in – Maurice Jan 08 '15 at 08:53
  • 2
    What a logfile would it be if you only keep the person's last message? I don't get it – halloei Jan 30 '15 at 12:51
  • you can just load the file, load the lines, find the line that starts with "
    Bob" and replace it with the new line. Seems like a very awkward way of running a chat program though. There'll be no history to lookup. its not really a log file if it doesn't log everything
    – roryok Jan 30 '15 at 12:51
  • Log file was not the right word to use, fixed it – Maurice Jan 30 '15 at 13:44
  • Load the file, load all the lines. Open a new tmp file. Loop over the lines. If a line does not start with `
    %username%` you write the line into the tmp file. Otherwise you `continue`. At the end you write the new chat message into the file. Overwrite the original file with the tmp file you just created. Done
    – AmazingDreams Jan 30 '15 at 13:59
  • 6
    But seriously, use a database. What if 2 processes are doing this at the same time? Databases take all these low-level worries away from you. – AmazingDreams Jan 30 '15 at 14:00
  • You should log every person's chat messages separately or for the best result you should save it in a database. If you want to find the last message of every person just run a SQL query for searching in date & time column. – mertyildiran Feb 02 '15 at 06:24

2 Answers2

2

PHP isn't my strong suit but hey you've got an open bounty so I decided to give it a shot. ;-) If you want to stay with your current IO approach, perhaps switch to use a bit of structured data in an XML file instead of plain text. If you would switch to XML, you could query and delete or modify the nodes as you needed. Here is a simple example to get you started. This example deletes the nodes which contain the 'John' value.

// http://php.net/manual/en/domdocument.load.php

//<?xml version="1.0" encoding="utf-8" ?>
//<root>
//<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>
//</root>

<?php
$doc = new DOMDocument();
$doc->load("chat.xml");
$xpath = new DOMXpath($doc);
$html = $doc->documentElement;
$elements = $xpath->query("//div[contains(b,'John')]");
if ($elements->length) {
    $html->removeChild($elements);
}
$doc->save("chat.xml");
?>
darlirium
  • 370
  • 4
  • 9
0

Thanks all, I ve found my answer here: how to replace a particular line in a text file using php?

About the database answers, yes a db is better, but I want to use SSE and I haven't found a working example with mysql and SSE. I want to push the data and all the examples I've found where using some kind of polling or a check-every3sec-for-new-data solution.

If someone has a tutorial or example proving it can be done, I would really like to see it (and then I need to upgrade my google skills, since I found nothing)

Community
  • 1
  • 1
Maurice
  • 1,147
  • 2
  • 21
  • 49
  • Not sure what you mean with SSE but depending on your actual use case a non-relational database like MongoDB might be a better fit than MySQL – Fabian Schmengler Feb 04 '15 at 08:38