tags:

views:

310

answers:

2

Hardest problem to explain:

$lol = "hello";
$that = "* music
* books
* games
* asos
* cry";

$lol = str_replace("*", $line, $that);

$name = str_replace(" ", "-", $line);
$myFile = "output/$name.csv";
$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = $lol;
fwrite($fh, $stringData);
fclose($fh);

I'm running through a big list of $lol variables and generating a text file for each of them, you can see the template for the text files in $that where * will become whichever $lol is in the loop.

So if $lol = "softlayer" I would get a csv with

softlayer music
softlayer books
softlayer games
etc..

Problem is when I open them in notepad they aren't the softlayer music, etc are all on the same line seperated by those horrible squares which mean 'new line'. And because of this the CSV's I'm generating are supposedly invalid according to the software I'm trying to use them with.

That was an absolutely horrible problem to explain so excuse me :)

Any help would be massively appreciated!

+1  A: 

Try "\r" instead?

philistyne
+8  A: 

In the mists of pre-history, the Windows Folk, Mac Folk, and Unix folk couldn't agree on what character(s) should denote the end of a line. On classic Mac OS, it was a carriage return (\r). On Unix, it was a newline (\n). On windows, it was a carriage return followed by a newline (\r\n).

Historically speaking, on old typewriters you had to do two things to get the head onto a new line. A carriage return (the big arm thing) would move the typewriter head back to the left side (or right side; shalom!) of the paper. However, the head would still be on the same line. You've have to engage a second mechanism (line feed or newline) to move the paper itself up a single line.

One of the reasons for the ASCII standard was to encode data that was going to be later printed out (via telegraph, teletype, etc.) so having both characters in the standard made sense. When it came time to decide on a standard for files whose primary purpose was to live in a computer, no one would could agree. The windows standard (\r\n) makes the most literal sense, but the Unix and Classic Mac standards (\n for Unix, \r for Classic Mac) saved a bit per-line, which was important in the early days.

This has caused confusion for decades, and will continue to cause confusion into the future. This is particularly true when using Unix tools in a windows environment. A lot of these tools assume a \n as a line ending. Additionally, it's possible your PHP source file is encoded with unix style line endings, which means a string separated over several lines is actually split up by \n newlines, and your program is behaving correctly.

Most modern text editors will figure out how a text file is encoded, and display the lines "as intended" by the author. Notepad doesn't work like that, and obeys what's literally in the text file.

Irrespective of all that, the following reg ex should normalize your line endings to what's standard on windows

$stringData = preg_replace('/[\n\r]{1,2}$/',"\r\n",$stringData);

Alternately, you could construct your string to end with carriage-return/line-feed

$string = "line 1\r\nline 2";
$string = "line 1\r\n".
          "line 2\r\n";
Alan Storm
Wouldn't that regex replace '\n\n' with '\r\n' (changing a double-newline to a single one)?What about preg_replace('/\r\n|\r|\n/', "\r\n", $stringData)
zildjohn01