Jump to content


Photo

*SOLVED* \n problem ...


  • Please log in to reply
5 replies to this topic

#1 leovn

leovn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 April 2006 - 02:08 AM

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 ?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 08 April 2006 - 02:11 AM

try:

<?php
echo "Testing :\r\n";
echo "A new line";
?>

follow me on twitter @PHPsycho

#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 08 April 2006 - 02:22 AM

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

echo "<pre>line one\nlinetwo";
or
echo "line one <br> line two";

New lines are not html and therefor unless the pre is around it, the browser will ignore them in the display.

#4 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 08 April 2006 - 03:44 AM

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

Windows: \n\r
Linux: \n
Mac: \r

\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.

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 April 2006 - 12:22 PM

If you are using this code:
<?php
echo "Testing :\n";
echo "A new line";
?>
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:
<?php

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

echo nl2br($str);

?>
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.

#6 leovn

leovn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 April 2006 - 12:35 AM

Thank you very much !!! It's work now , thanks all again ^^




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users