datafan Posted February 6, 2008 Share Posted February 6, 2008 When I use the following delete reservation script it works perfect except it removes the \n newline character from the last line of my flat file. That screws up the next line to be written to the file. Any idea? I tried to add a \n and ended up with a empy file! yikes <?php if ($_POST['submit']) { $classname = $_POST['classname']; $keyword = $_POST['keyword']; $filename = "flatfolder/masterflat.txt"; $file_array = file($filename); foreach ($file_array as $file_line) { $file_line = explode("|", trim($file_line)); if ($file_line[4] != $classname && $file_line[6] != $keyword) { $new_file_array[] = implode("|", $file_line); } } $open_file = fopen($filename, 'w'); fwrite($open_file, implode("\n", $new_file_array)); fclose($open_file); unlink("reservations/$classname"); echo "The reservation has been removed successfully.<br />"; } ?> This is what I tried but no workie. fwrite($open_file, implode("\n", $new_file_array."\n")); Quote Link to comment https://forums.phpfreaks.com/topic/89812-solved-n-getting-removed-from-last-line-of-flat-file/ Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 You almost had it, instead of <?php fwrite($open_file, implode("\n", $new_file_array."\n")); ?> do <?php fwrite($open_file, implode("\n", $new_file_array)."\n"); ?> Notice that you have to add the "\n" to the string created by the implode(). Ken Quote Link to comment https://forums.phpfreaks.com/topic/89812-solved-n-getting-removed-from-last-line-of-flat-file/#findComment-460241 Share on other sites More sharing options...
datafan Posted February 6, 2008 Author Share Posted February 6, 2008 WooooHoooo! works perfect, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/89812-solved-n-getting-removed-from-last-line-of-flat-file/#findComment-460288 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.