RE: [PHP] replacing a line in a file
the point is that you can't replace part of a flat file. you can append to the content or replace it all. if you want to replace part, you need to read it all, amend the relevant line and then replace it all. Tim Ward Senior Systems Engineer Please refer to the following disclaimer in respect of this message: http://www.stivesdirect.com/e-mail-disclaimer.html -Original Message- From: enthalpy [mailto:[EMAIL PROTECTED]] Sent: 13 March 2001 23:56 To: Henrik Hansen Cc: php general Subject: Re: [PHP] replacing a line in a file that could work but i dont want to append the new line a the bottom which it looks like thats what your doing. i need to replace the line where it is -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- On Wed, 14 Mar 2001, Henrik Hansen wrote: maybe this? while($line = fgets($fp, 1024)) { $line = str_replace("replace_this", "with_this", $line); $content .= $line; } fwrite($fp, $content); -- Henrik Hansen - Original Message - From: "enthalpy" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, March 14, 2001 12:29 AM Subject: [PHP] replacing a line in a file having problems replacing a line in a file. need to search the whole document for a string and then replace that line with a string. any ideas? this is causing lots of problems now. $fpoint3 = fopen ($testshadow, "r+") or die("couldnt open shadow file again"); $contents = fread ($fpoint3, $shadowfilesize); fputs ($fpoint3, ereg_replace("$data[0]:$shadow[1]", "$data[0]:$data[1]", $contents)); fclose ($fpoint3); any ideas? -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] replacing a line in a file
Hey! I didn't try to analyze your code, but why not using the file() function which opens a file and puts it's content into an array. After doing that, you could seek that array line-by-line for "$data[0]:$shadow[1]" and replace it with $data[0]:$data[1]. Since you're dealing with full lines in your script, file() is the best choice (though, sometimes not the fastest). It will also improve your code by making it much easier to read than when using fopen(), fread(), etc.. as you're doing now. Best Regards - Aviv Revach At 17:29 13/03/01 -0600, enthalpy wrote: having problems replacing a line in a file. need to search the whole document for a string and then replace that line with a string. any ideas? this is causing lots of problems now. $fpoint3 = fopen ($testshadow, "r+") or die("couldnt open shadow file again"); $contents = fread ($fpoint3, $shadowfilesize); fputs ($fpoint3, ereg_replace("$data[0]:$shadow[1]", "$data[0]:$data[1]", $contents)); fclose ($fpoint3); any ideas? -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] XOR-Coders Mega Programming Resource Site! - http://members.xoom.com/xorcoders/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] replacing a line in a file
maybe this? while($line = fgets($fp, 1024)) { $line = str_replace("replace_this", "with_this", $line); $content .= $line; } fwrite($fp, $content); -- Henrik Hansen - Original Message - From: "enthalpy" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, March 14, 2001 12:29 AM Subject: [PHP] replacing a line in a file having problems replacing a line in a file. need to search the whole document for a string and then replace that line with a string. any ideas? this is causing lots of problems now. $fpoint3 = fopen ($testshadow, "r+") or die("couldnt open shadow file again"); $contents = fread ($fpoint3, $shadowfilesize); fputs ($fpoint3, ereg_replace("$data[0]:$shadow[1]", "$data[0]:$data[1]", $contents)); fclose ($fpoint3); any ideas? -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] replacing a line in a file
that could work but i dont want to append the new line a the bottom which it looks like thats what your doing. i need to replace the line where it is -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- On Wed, 14 Mar 2001, Henrik Hansen wrote: maybe this? while($line = fgets($fp, 1024)) { $line = str_replace("replace_this", "with_this", $line); $content .= $line; } fwrite($fp, $content); -- Henrik Hansen - Original Message - From: "enthalpy" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, March 14, 2001 12:29 AM Subject: [PHP] replacing a line in a file having problems replacing a line in a file. need to search the whole document for a string and then replace that line with a string. any ideas? this is causing lots of problems now. $fpoint3 = fopen ($testshadow, "r+") or die("couldnt open shadow file again"); $contents = fread ($fpoint3, $shadowfilesize); fputs ($fpoint3, ereg_replace("$data[0]:$shadow[1]", "$data[0]:$data[1]", $contents)); fclose ($fpoint3); any ideas? -CoreComm-Internet-Services---http://core.com- (Jon Marshall CoreComm Services Chicago) ([EMAIL PROTECTED] Systems Engineer II) ([EMAIL PROTECTED] Network Operations) -Enthalpy.org-http://enthalpy.org- ([EMAIL PROTECTED] The World of Nothing) -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]