PHP not recognizing new line character "\n" when writin

PHP new line character \n when writing to a file.

Page 1 of 1

3 Replies - 20474 Views - Last Post: 16 January 2009 - 10:04 AM Rate Topic: -----

#1 tem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-January 09

PHP not recognizing new line character "\n" when writin

Posted 15 January 2009 - 05:13 AM

<code>
$ourFileName = "test.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, "111111\n");
fwrite($ourFileHandle, "222222\n");
fclose($ourFileHandle);
</code>

When opening the file after running above code there is only one line
"111111222222"

Is there something else i am missing here?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP not recognizing new line character "\n" when writin

#2 webwritersgroup  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 14-January 09

Re: PHP not recognizing new line character "\n" when writin

Posted 15 January 2009 - 06:00 AM

:^: The code looks right, so I copied your code and tried it out. It worked fine for me.

What are you viewing the text file with? Use something like WordPad (windows) to view the file...something that supports the formatting. If you use a basic app like notepad, I don't think the formatting (line breaks) will come through.
Was This Post Helpful? 1

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,614
  • Joined: 08-August 08

Re: PHP not recognizing new line character "\n" when writin

Posted 15 January 2009 - 06:08 AM

If you're reading the file in php and viewing it in a browser, you need to use <pre></pre> tags or the nl2br() function on it.

http://www.php.net/nl2br
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP not recognizing new line character "\n" when writin

Posted 16 January 2009 - 10:04 AM

Also, if you're on a Windows operating system, you have to make it \r\n instead of \n, as Windows's newlines are always preceded by a carriage return.

This post has been edited by Valek: 16 January 2009 - 10:05 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1