Jump to content

[SOLVED] \n getting removed from last line of flat file


datafan

Recommended Posts

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"));

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.