Welcome to WebmasterWorld Guest from 35.172.111.215

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

editing text files with php

text edit with php online

     
2:41 am on Oct 31, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 20, 2003
posts:19
votes: 0


I was directed here by korkus because I first asked this in the html forum and the answer was no. I cant do it with HTML so:

I want to edit a .txt file using php.
The scenario is this:

When a window is closed by a user of my site I want the php to find a .txt file and remove a line(s) from it. Is this possible?

If not is it possible any other way you can think of?

Any help or pushing in the right direction would be great.

7:17 am on Oct 31, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 7, 2003
posts:505
votes: 0


harrybailey,

<If not is it possible any other way you can think of?>

Yes..

- Open the file for read.
- Read the file line-by-line into an array.
- Close the file.
- Open the file for write.
- Loop beginning with the first value of the array and ending with the last.
- Check each value of the array to see if it's the one you want to delete. If it's not, then write it back to the file. If it is, then don't.
- Close the file.

7:24 am on Oct 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


Easy enough to do, it would seem -- i.e., PHP is fully able to read and write files, but a couple of questions to clarify what you are asking...where is this text file getting lines from and how should the script know which ones to delete?

The basic idea would be...

<?php 
$fname = "test.txt";
$exclude = "some string";
$lines = file($fname);
$out = "";
foreach ($lines as $line) {
if (strstr($line, $exclude) == "") {
$out .= $line;
}
}
$f = fopen($fname, "w");
fwrite($f, $out);
fclose($f);
?>

Jordan

7:34 am on Oct 31, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


this is the bit that troubles me
When a window is closed by a user of my site

php has no idea when you close the browser
8:32 am on Oct 31, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 7, 2003
posts:505
votes: 0


jatar_k,

<<When a window is closed by a user of my site>>

<php has no idea when you close the browser>

It does if you use the onUnload event to trigger a PHP script.

8:35 am on Oct 31, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


js/php combo but not just php
9:56 am on Oct 31, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 20, 2003
posts:19
votes: 0


Ok, this is all good stuff.
The actual use is for a java chatroom. At present is saves the chat box and the users in the room to two text files. When you close the window and reopen it, the user name is still there and noone knows you have left.

I want the php to edit the text file to remove a users name from the list when they leave.(close browser window)

If there is a php onUnload then great, if not I could just have an html small popup with php in it open when the browser window is closed.

The text file is in the same folder as the chat page.

4:31 pm on Oct 31, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 7, 2003
posts:505
votes: 0


harrybailey,

<The actual use is for a java chatroom. At present is saves the chat box and the users in the room to two text files. When you close the window and reopen it, the user name is still there and noone knows you have left>

If this is the use, then you may have another issue.

If the user closes the window all happy the way you intend, then no problem. But there are other circumstances under which this won't work. For instance, they could get booted offline with the window left open, or their machine could crash. In either of these two cases, they would technically no longer be in the chatroom, but their name would still be in your text file.

You may want to see if you can setup time sort of polling system. For instance, you have javascript in the chat window that, once per minute, contacts the server and says "I'm still here." If the server doesn't receive that, then it removes the user from the text file.

This way, if their machine crashes or they get booted offline before they can close the window properly, it will still mark them offline in no more than 60 seconds. Of course, you could make this happen every 10 seconds if you want. The more frequently you do it, the more up-to-date the list of people online will be, but the more strain it would put on your server and its bandwidth.

One way to accomplish this may be to have a 0-pixel frame (can you have a frame that's 0 pixels?, if not, then 1) at the top of the chat window. In this frame is code that can meta-refresh every x seconds sending over an "I'm still here" signal with an ID unique to that person logged in, so the server knows who "I" am when sending the signal.

8:36 pm on Oct 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


Good ideas NickCoons.

Always better to code for the worst case and hope for the best. ;)

Jordan

1:40 am on Nov 1, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 7, 2003
posts:505
votes: 0


MonkeeSage,

<Always better to code for the worst case and hope for the best. ;)>

I've written programs like this before, and as much as it would be nice for users to cleanly close programs when they're done.. it's almost never going to *actually* happen that way :-).

11:31 pm on Nov 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 15, 2003
posts:55
votes: 0


If it is a Java Chat, I would be tempted to handle it with a servlet before PHP. Having multiple people read and write to a file is inefficient. If I were working on such a program, I would prefer a file that holds all the current users in memory and save the disk io. Seems more like a servlet application than PHP.