469,306 Members | 2,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

replace string in file

Hi!

I am using PHP4.3.3 on a windows XP platform.

I am building a small program that will allow my students to subscribe and
unsubscribe from a service that will let them know when new results of their
exams are added to the webpage of school.
Since I am not allowed to use mySQL as a database, I am using a simple flat
text-file to store the emailadresses submitted by my students.
I can already add addresses to the file, even after I have checked if they
already exist.

But now I am working at the unsubscribe-part of the script, and I have
simply no idea how to remove the string from the file.
Anybody have any ideas? Please keep in mind that I am total newbie to PHP
(although I am already a little proud to have achieved so much in so little
time without any help so far).

The text-file looks like this:
a_****@domain.com,
an*************@anotherdomain.com
etc
But I can easily adjust the "write-email"-part of my script.

TIA

Niels Bosboom.
Jul 17 '05 #1
7 19347
On Sat, 27 Sep 2003 13:46:55 GMT, "Niels Bosboom" <n.*******@wanadoo.nl> wrote:
I am using PHP4.3.3 on a windows XP platform.

I am building a small program that will allow my students to subscribe and
unsubscribe from a service that will let them know when new results of their
exams are added to the webpage of school.
Since I am not allowed to use mySQL as a database, I am using a simple flat
text-file to store the emailadresses submitted by my students.
Out of interest, why aren't you allowed to use a database? By using a flat
file you end up hitting all the many problems that databases were invented to
solve in the first place.
I can already add addresses to the file, even after I have checked if they
already exist.

But now I am working at the unsubscribe-part of the script, and I have
simply no idea how to remove the string from the file.


You have to read in the whole file, and then write out to to another file,
excluding the one entry you want to remove, and then replace the original with
the new one.

(You also have to make sure nobody changes the original file whilst you're
doing this, and that nobody is reading the file when you replace it).

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
I noticed that Message-ID: <4m********************************@4ax.com>
from Andy Hassall contained the following:

You have to read in the whole file, and then write out to to another file,
excluding the one entry you want to remove, and then replace the original with
the new one.

(You also have to make sure nobody changes the original file whilst you're
doing this, and that nobody is reading the file when you replace it).


I've not tried this (I agree that a database would be the best idea but
as a teacher myself, I know what it can be like in schools), but could
you not write the unsubscribe addresses to another file?

Then, when you send out the email, send mails from file a as long as
they are not contained in file b. In time this could become unwieldy
and inefficient but I guess you would be starting from scratch each year
anyway.

Alternatively, why not see if some kind person will allow you some
database space?

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
Niels Bosboom wrote:
But now I am working at the unsubscribe-part of the script, and I have
simply no idea how to remove the string from the file.
Anybody have any ideas? Please keep in mind that I am total newbie to PHP
(although I am already a little proud to have achieved so much in so
little time without any help so far).


Hello Niels,

Besides the suggestions posted by others, you could also choose to put each
separate subscription in a separate file, together in one directory. In
other words, a file then is a record more or less and the directory is the
table. This reduces the need for locking and you don't have to rewrite the
whole list every time, just the "record" you're interested in.

I've used this approach myself some time ago with some Perl based CGI and it
works well.

Best regards,

Eric

Jul 17 '05 #4

"Niels Bosboom" <n.*******@wanadoo.nl> wrote in message
news:jx***********************@castor.casema.net.. .
Hi!

I am using PHP4.3.3 on a windows XP platform.

I am building a small program that will allow my students to subscribe and
unsubscribe from a service that will let them know when new results of their exams are added to the webpage of school.
Since I am not allowed to use mySQL as a database, I am using a simple flat text-file to store the emailadresses submitted by my students.
I can already add addresses to the file, even after I have checked if they
already exist.

But now I am working at the unsubscribe-part of the script, and I have
simply no idea how to remove the string from the file.
Anybody have any ideas? Please keep in mind that I am total newbie to PHP
(although I am already a little proud to have achieved so much in so little time without any help so far).

The text-file looks like this:
a_****@domain.com,
an*************@anotherdomain.com
etc
But I can easily adjust the "write-email"-part of my script.

TIA

Niels Bosboom.


// UNTESTED!
// email to remove is in $_POST['email']

$filearray = file ( 'list.txt' );

$newarray = array();

foreach ($filearray as $email ){
if ( $email != $_POST['email'] )
array_push ( $newarray, $email );
}

$string = implode ( "\n", $newarray );
$fp = fopen ( 'new.txt', w );
fwrite ( $fp , $string );
fclose ( $fp );
Jul 17 '05 #5
"Eric Veltman" <er**@veltman.nu> wrote in message
news:vn************@corp.supernews.com...
Niels Bosboom wrote:
But now I am working at the unsubscribe-part of the script, and I have
simply no idea how to remove the string from the file.
Anybody have any ideas? Please keep in mind that I am total newbie to PHP (although I am already a little proud to have achieved so much in so
little time without any help so far).
Hello Niels,

Besides the suggestions posted by others, you could also choose to put

each separate subscription in a separate file, together in one directory. In
other words, a file then is a record more or less and the directory is the
table. This reduces the need for locking and you don't have to rewrite the
whole list every time, just the "record" you're interested in.

I've used this approach myself some time ago with some Perl based CGI and it works well.


It's a good idea. The Ultimate Bulletin Board used this method for handling
the forum topics with each one being written into a different file. Each
member's information was also a separate file. I had a 6,000 member forum
with 12,000 topics and never had a problem with overloading even though the
whole file structure for the forum amounted to 90,000 files. If your school
doesn't like thousands of small files on the file system maybe they'll give
you a database instead. ;)
Jul 17 '05 #6
Thank you all (Andy, Geoff, Jason, Eric, Paulus and all others who have read
my question), for replying so swiftly!

I will work on this problem today to see if I can alter Jason's script and
combine it with the ideas of others.

thankx again!

Niels.
Jul 17 '05 #7
> // UNTESTED!
// email to remove is in $_POST['email']

$filearray = file ( 'list.txt' );

$newarray = array();

foreach ($filearray as $email ){
if ( $email != $_POST['email'] )
array_push ( $newarray, $email );
}

$string = implode ( "\n", $newarray );
$fp = fopen ( 'new.txt', w );
fwrite ( $fp , $string );
fclose ( $fp );


After some testing, the following is the script I am currently using. It is
working well! Thankx Jason and all others!
gr Niels.

---------------------------

function verwijderadres($adres,$bestand)
{
$filearray = file ($bestand);
$newarray = array();
$adresmetregeleinde = $adres.",";
foreach ($filearray as $email )
{
if( !stristr( $email, $adresmetregeleinde) )
array_push ( $newarray, $email );
}
$string = implode ( "", $newarray );
$fp = fopen($bestand,"w");
fwrite ( $fp , $string );
}
---------------------------
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by hokieghal99 | last post: by
3 posts views Thread by RickN | last post: by
1 post views Thread by Michael Yanowitz | last post: by
17 posts views Thread by Levidikus | last post: by
3 posts views Thread by mouac01 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.