Jump to content

Raw text file line search & destroy


HappyDude

Recommended Posts

Hello everyone,

This is my first post here.  Cheers for any advice!

I have written a little php script that interfaces directly with phpIcalendar.
The calendar is being used as a volunteer sign up sheet for community events at the local fire department.

It allows firefighters to post upcoming events on specific days, with description, location, times, etc... and then visitors to the site can sign up for events they want to volunteer their help at.  The volunteer sign up includes fields for their name, the time they can start helping, how long they can stay, their phone number, etc...

Pretty simple stuff, and [i]so far[/i] it's been working great.

The calendar uses two raw text files to save the event and volunteer data.
One text file contains the event information, while the other contains all the volunteer sign up information.

When new events are entered or volunteers sign up, my script simply opens the corresponding text file, and appends the new data onto the end.

Here's the down side.
When someone types in the wrong data for an event, and wants to erase the event from the calendar, they have to call me, and I have to manually FTP into the web server, edit the text file, and re-upload it to remove the wrong event data.

What I would like is to have a "click here to erase this event" button on each event so that it can be automatically erased without the need to track me down.

The erasure process would of course be password protected.  I have no problem with that part, but what I need help with is the actual erasure of specific lines in the text file.  Since every time a new event is added, it is appended to the end of the file (with a line break in between) the file is fairly long, and has lots of events in it.

I have been experimenting, and have managed to get to the point where the text file is read into an array, I can type in a line number, and it will erase that line number, then rewrite the text file (minus the erased line number).

However, without knowing the exact line number the specific event data is contained on, erasing lines like this is useless.

What I need help with is, how do you "search" the text file for specific data, and erase just that?

Here's a way I was thinking of doing it (I just don't know if it's viable, or how the code would look):

The two fields in the text file are:

date:xxxxxx [i](where xxxxxx is the date in mmddyy format)[/i]
description:xxxxxx [i](where xxxxxx is the description of the event[/i]

The user would click on "erase this event" which would pass the already loaded event description, and numeric date to the "erase.php" script.

The script would then open the text file, load it into an array, and proceed to search for the line "date:xxxxxx" that matched the date of the event to be erased.

If the date [b]AND[/b] the description directly below that date matched the date and description of the event that was to be erased, then those line numbers would be placed into variables, which would then be used to erase those line numbers and rewrite the text file (minus the erased lines).

I'd like it to match both fields (data and description), because there are many events with the same description, but one one event per date.
But if it would be hundreds of times easier to simple search for the date field, and erase it, I'll do that too!

---


I hope that makes some sense...  :D
Is this how one would go about finding and erasing specific lines in a text file?


Thank you for any information!
I'm not asking for anyone to 'do it for me', I just want to make sure I'm going in the right direction before spending lots of time trying to figure it out. :)
Link to comment
https://forums.phpfreaks.com/topic/27152-raw-text-file-line-search-destroy/
Share on other sites

1) Why dont you use a database?

2) You could do the following
[code]
$filename = "events.txt";
$f_handle = fopen($filename,"r");
$contents = fread($f_handle,filesize($filename));

$search_text = "Test Event";

$contents = eregi_replace ($search_text," ",$contents);

fclose($f_handle);

$f_handle = fopen($filename,"w");
fwrite($f_handle,$contents);

fclose($f_handle);
[/code]

check the wrtie() function not sure if thats the correct syntax
Glenelkins you're brilliant mate.  ;D

Works like a charm.
[b]eregi_replace[/b] was exactly what I needed.

I've got the script working 100% now, erasing the exact event entry (well, replacing the entry with a blank)  :)

Cheers for your quick help!

As for the database, I'd love to use MySQL for this.  Unfortunately, the website runs on a town web server, and there is no chance to getting a database engine to use (completely out of my hands).


Thanks again, I hope this snippet of code helps others down the road!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.