aleksandra Posted January 20, 2008 Share Posted January 20, 2008 Hello I need to change the last line of an external text file The file consists of Last Connection: 1/20/2008 1:44:08 PM Indoor Temperature: 73 Outdoor Temperature: 21 Indoor Setpoint: 72 New Setpoint: 55 I need to know how to change the last line (replace it with a user input) Thank you Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/ Share on other sites More sharing options...
janim Posted January 20, 2008 Share Posted January 20, 2008 i think you need to call this file like this first : $open = fopen($file,"a+"); $str = explode("\n",$open); if i'm right your last like is $str[4] then rewrite the same file but change $str[4] to new value if that what you mean Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444635 Share on other sites More sharing options...
aleksandra Posted January 20, 2008 Author Share Posted January 20, 2008 i'm going in circles can't make it work Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444660 Share on other sites More sharing options...
aleksandra Posted January 20, 2008 Author Share Posted January 20, 2008 any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444663 Share on other sites More sharing options...
teng84 Posted January 20, 2008 Share Posted January 20, 2008 can we have your current codes? Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444664 Share on other sites More sharing options...
revraz Posted January 20, 2008 Share Posted January 20, 2008 You can't just change one line, you have to read it all in, and write a new file with the new line. Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444665 Share on other sites More sharing options...
aleksandra Posted January 20, 2008 Author Share Posted January 20, 2008 ok this is how i started it trying to separate lines and only change the last one and i'm very stuck <?php $file_handle = fopen("filename.txt", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode('=', $line_of_text); print $line_of_text. "<BR>"; } fclose($file_handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444667 Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 $lines=file('filename.txt'); array_pop ($lines); array_push ($lines,"new text"); file_put_contents('filename.txt',$lines); Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444748 Share on other sites More sharing options...
aleksandra Posted January 21, 2008 Author Share Posted January 21, 2008 Ok this does what i need it to do I suck at php so i'm sure this code is messy as hell so if anyone can clean it up or make it work for an unknown number of lines in a text file that would be great <?PHP $text ="The new set point is: ". $_POST['shit']; $myFile = "file.txt"; $lines = file($myFile); $fh = fopen($myFile, 'w') or die("Can not open file"); foreach ($lines as $line_num => $line) { } $stringData =$lines[0].$lines[1].$lines[2].$lines[3].$text; fwrite($fh, $stringData); $lines = file('file.txt'); // Loop through our array foreach ($lines as $line_num => $line) { echo ($line);?><br/> <?php } ?> <form action="process1.php" method="post"> <textarea name="shit"></textarea> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444767 Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 eeewwww HAHA <? $text ="The new set point is: ". $_POST['shit']; $lines=file('file.txt'); array_pop ($lines); array_push ($lines,"new text"); file_put_contents('file.txt',$lines); echo implode("<br>",$file); ?> <form action="process1.php" method="post"> <textarea name="shit"></textarea> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444778 Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 Edit <? $text ="The new set point is: ". $_POST['shit']; $lines=file('file.txt'); array_pop ($lines); array_push ($lines,"$text"); file_put_contents('file.txt',$lines); echo implode("<br>",$lines); ?> <form action="process1.php" method="post"> <input type=text name="shit" size=5> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/86961-replace-or-delete-a-line-in-an-external-text-file/#findComment-444791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.