extra lines in text file function str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

extra lines in text file function str_replace

Post 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!
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: extra lines in text file function str_replace

Post 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?
Post Reply