searching and replacing in a file

This is a discussion on searching and replacing in a file within the PHP General forums, part of the PHP Programming Forums category; How can I read through a file and find a string and then replace it with a new string and ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-14-2003
Richard Kurth
 
Posts: n/a
Default searching and replacing in a file

How can I read through a file and find a string and then replace it with a
new string and then save the whole file.

Below is a peace of what I need to search through. I need to find the string
subscribe_policy = open and replace it with subscribe_policy = open+confirm.
Then save the whole file.

I have tried str_replace but it just wipes out the original file.

$file = "/var/lib/majordomo/domain.com/lists/listname.config";
$fp = fopen("$file", "w+");
$content = fread($fp, filesize($file));
$replace= "subscribe_policy = open+confirm";
$search_str = "subscribe_policy = open";
$content2 = str_replace($search_str,$replace, $content);
fwrite($fp,$content2);
fclose($fp);

# restrict_post [restrict_post] (autoupdates) <resend>
# If defined, only addresses listed in these files (colon or space
# separated) can post to the mailing list. By default, these files
# are relative to the lists directory. These files are also checked
# when get_access, index_access, info_access, intro_access,
# which_access, or who_access is set to 'list'. This is less useful
# than it seems it should be since there is no way to create these
# files if you do not have access to the machine running resend.
# This mechanism will be replaced in a future version of
# majordomo/resend.
restrict_post = autoupdates.posters

# subscribe_policy [enum] (open) <majordomo> /open;closed;auto;op
# One of three values: open, closed, auto; plus an optional
# modifier: '+confirm'. Open allows people to subscribe themselves
# to the list. Auto allows anybody to subscribe anybody to the list
# without maintainer approval. Closed requires maintainer approval
# for all subscribe requests to the list. Adding '+confirm', ie,
# 'open+confirm', will cause majordomo to send a reply back to the
# subscriber which includes a authentication number which must be
# sent back in with another subscribe command.
subscribe_policy = open

# unsubscribe_policy [enum] (closed) <majordomo> /open;closed;auto/
# One of three values: open, closed, auto. Open allows people to
# unsubscribe themselves from the list. Auto allows anybody to
# unsubscribe anybody to the list without maintainer approval. The
# existence of the file <listname>.auto is the same as specifying
# the value auto. Closed requires maintainer approval for all
# unsubscribe requests to the list. In addition to the keyword, if
# the file <listname>.closed exists, it is the same as specifying
# the value closed. The value of this keyword overrides the value
# supplied by any existent files.
unsubscribe_policy = open
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0