<snip>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.
Have php...
1) Make a copy of the original:
if ( ! copy ( $old_file, $backup_file . ".bak" ) ) {
// Uh-oh, there's an error
}2) Read the original and open a temp file:
$file_contents = file ( $old_file );
if ( ! $fp = fopen ( $temp_file, "w+" ) ) {
// Oh my, can't open the file
}3) Parse and write:
for ( $i = 0; $i < sizeof ( $file_contents ); $i++ ) {
$write_this = preg_replace ( $pattern, $replacement, $file_contents[$i] );
if ( ! fwrite ( $fp, $write_this ) ) {
// Darn, can't write to the temp file
}
}4) Cleanup and copy temp file:
fclose ( $fp );
if ( ! copy ( $temp_file, $old_file ) ) {
// Zoinks, there seems to be a problem
}It's 5:00 am for me, and I'm typing all this from memory, so there may be bugs :) You can remove the temp file if you so desire if the above runs without error.
-- By-Tor.com It's all about the Rush http://www.by-tor.com
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

