473,441 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

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 19550
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
17
by: Levidikus | last post by:
Normally, I never have any problems with String.Replace(). However, I found that I need to replace multiple instances of the character "ª" (\xAA) with a # symbol. The input file is a simple one...
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.