Page 1 of 1

extra lines in text file function str_replace

Posted: Tue Jan 18, 2011 1:05 am
by shanehunter
Below is my code:

Code: Select all

function TransferLine($link,$fromfile,$tofile) 
{	
	if(file_exists($fromfile) && file_exists($tofile))
	{
		$holdcontents = file_get_contents($fromfile);
		$holdcontents2 = str_replace($link, "", $holdcontents);
		$holdcontents2 = str_replace("\n", "\n", $holdcontents2);
		file_put_contents($fromfile, $holdcontents2);
		$fileContents = $link . PHP_EOL;			
		file_put_contents($tofile, $fileContents, FILE_APPEND);
	} 
	else 
	{
		$string = "FileNotExist";
	};
	return $string;
}
Here is my problem:

This code is inserting a line of text (encoded urls) from one text file to another. This part works correctly, but the problem that I'm having is it's also inserting an empty line break in the new file, every time it moves the line of text to the new file. I need it to just insert the lines, without extra line breaks. Help is needed, and greatly appreciated!

Re: extra lines in text file function str_replace

Posted: Tue Jan 18, 2011 1:57 am
by Peter Kelly
I don't do much file manipulation so could you just confirm why you have put the line.

Code: Select all

$holdcontents2 = str_replace("\n", "\n", $holdcontents2);
as on a quick glance during work, All I can tell your doing is replacing \n with \n which seems a bit pointless?