Connecting Tech Pros Worldwide Forums | Help | Site Map

replace string in file

Niels Bosboom
Guest
 
Posts: n/a
#1: Jul 17 '05
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_name@domain.com,
another_student@anotherdomain.com
etc
But I can easily adjust the "write-email"-part of my script.

TIA

Niels Bosboom.



Andy Hassall
Guest
 
Posts: n/a
#2: Jul 17 '05

re: replace string in file


On Sat, 27 Sep 2003 13:46:55 GMT, "Niels Bosboom" <n.bosboom@wanadoo.nl> wrote:
[color=blue]
>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.[/color]

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.
[color=blue]
>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.[/color]

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 (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Geoff Berrow
Guest
 
Posts: n/a
#3: Jul 17 '05

re: replace string in file


I noticed that Message-ID: <4m6bnvoo5c4sa7flna6d47fkp4hpd83g4k@4ax.com>
from Andy Hassall contained the following:
[color=blue]
>
> 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).[/color]

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/
Eric Veltman
Guest
 
Posts: n/a
#4: Jul 17 '05

re: replace string in file


Niels Bosboom wrote:
[color=blue]
> 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).[/color]

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

Jason
Guest
 
Posts: n/a
#5: Jul 17 '05

re: replace string in file



"Niels Bosboom" <n.bosboom@wanadoo.nl> wrote in message
news:jxgdb.358684$lA6.11687607@castor.casema.net.. .[color=blue]
> 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[/color]
their[color=blue]
> exams are added to the webpage of school.
> Since I am not allowed to use mySQL as a database, I am using a simple[/color]
flat[color=blue]
> 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[/color]
little[color=blue]
> time without any help so far).
>
> The text-file looks like this:
> a_name@domain.com,
> another_student@anotherdomain.com
> etc
> But I can easily adjust the "write-email"-part of my script.
>
> TIA
>
> Niels Bosboom.
>
>
>[/color]

// 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 );


Paulus Magnus
Guest
 
Posts: n/a
#6: Jul 17 '05

re: replace string in file


"Eric Veltman" <eric@veltman.nu> wrote in message
news:vnbtham90pr0c2@corp.supernews.com...[color=blue]
> Niels Bosboom wrote:
>[color=green]
> > 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[/color][/color]
PHP[color=blue][color=green]
> > (although I am already a little proud to have achieved so much in so
> > little time without any help so far).[/color]
>
> Hello Niels,
>
> Besides the suggestions posted by others, you could also choose to put[/color]
each[color=blue]
> 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[/color]
it[color=blue]
> works well.[/color]

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. ;)


Niels Bosboom
Guest
 
Posts: n/a
#7: Jul 17 '05

re: replace string in file


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.


Niels Bosboom
Guest
 
Posts: n/a
#8: Jul 17 '05

re: replace string in file


> // UNTESTED![color=blue]
> // 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 );[/color]

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 );
}
---------------------------


Closed Thread