Jump to content

*SOLVED* \n problem ...


leovn

Recommended Posts

Look at the source code in your browser...the newline is there, it just doesn't print them in the html display...either do:

[code]echo "<pre>line one\nlinetwo";[/code]
or
[code]echo "line one <br> line two";[/code]

New lines are not html and therefor unless the pre is around it, the browser will ignore them in the display.
Link to comment
Share on other sites

ToonMariner's got something, too... different operating systems have different new line characters:

[code]Windows: \n\r
Linux: \n
Mac: \r[/code]

\n is new line, while \r is carriage return - Windows apps usually require both. And you can also check out the nl2br function - you give it a string that contains new line characters, and it will return a string with HTML line breaks in their place. :)

the <pre> tag is a personal favorite of mine - especially for print_r and var_dump. Without <pre>, the output from two functions gets messy fast.
Link to comment
Share on other sites

If you are using this code:
[code]<?php
echo "Testing :\n";
echo "A new line";
?>[/code]and viewing the page in a web browser then it wont show like so:
Testing :
A new line

However it will if you go to View -> Source Code in your web browser. Web browsers ignore whitespace characters such as \n, \r \t etc. This is why your text appears in one line. To resolve this you will have to tell your browser to put in a new line after Testing: . Using the line break tag manualy. Or you could do this:
[code]<?php

$str = "Testing: \n
A new line";

echo nl2br($str);

?>[/code]With nl2br it doesn't mater whether you use \r or \n as it will convert both into <br /> automatically. However if you are writting the output to a file you will need to use \r\n for Windows, \r for Mac and \n for Linux as others have pointed out above.
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.