Jump to content

Archived

This topic is now archived and is closed to further replies.

leovn

*SOLVED* \n problem ...

Recommended Posts

My code was :
<?php
echo "Testing :\n";
echo "A new line";
?>

Then it's ouput : Testing :A new line ... What should i do to make a new line ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Thank you very much !!! It's work now , thanks all again ^^

Share this post


Link to post
Share on other sites

×

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.