lalabored Posted May 5, 2007 Share Posted May 5, 2007 I have a file named lala.php. I have a script to check if the first line is "lalala", if it isn't then it will replace it with "lalala". Then it will rewrite the new information back into lala.php. For some reason this doesn't work. It will simply replace the contents of the entire file with "lalala". I only want it to replace the first line with "lalala". $fh = fopen("lala.php", "w") or die("Error: Cannot open file."); $check = file("lala.php"); $lala = "lalala"; if($check[0] != $lala){ $check[0] = $lala; $newcheck = implode("",$check); fwrite($fh, $newcheck); } fclose($fh); Link to comment https://forums.phpfreaks.com/topic/50084-solved-replace-line/ Share on other sites More sharing options...
benjaminbeazy Posted May 5, 2007 Share Posted May 5, 2007 have u tried fopen(FILE, r+); to read and write w truncates file length to 0 Link to comment https://forums.phpfreaks.com/topic/50084-solved-replace-line/#findComment-245857 Share on other sites More sharing options...
lalabored Posted May 5, 2007 Author Share Posted May 5, 2007 Thank you! It works! Link to comment https://forums.phpfreaks.com/topic/50084-solved-replace-line/#findComment-245859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.