Solved

\n problem

Posted on 2002-07-11
9
188 Views
Last Modified: 2006-11-17
Can somebody explain to me why php doesn't replace \n with a linefeed?  When I print text that contains \n, the whole string appears on the same line and all the \n are visible. Why?
0
Comment
Question by:huxun
  • 4
  • 4
9 Comments
 

Author Comment

by:huxun
Comment Utility
Multiple post ... please delete!
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Something to be aware of (you may already know), if you want to show html on several lines and you are using \n to terminate lines, you can use the nl2br function to put in <br> instead of \n.

Regards,

Richard Quadling.
0
 
LVL 2

Expert Comment

by:egomaster
Comment Utility
I recall having a similar problem that is having \n being printed instead of being line feeds.
Can you please post your PHP code and I might remember what was wrong.
0
 

Author Comment

by:huxun
Comment Utility
I have already solved this problem with help from some experts on another thread.  As I explained, this is a multiple post, due to a refresh. I really think that bug should be fixed.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
<html>
<body>
<?php

echo 'This is on line 1.\nThis is on line 2.\n';

echo "This is on line 3.\nThis is on line 4.\n";

echo nl2br('This is on line 5.\nThis is on line 6.\n');

echo nl2br("This is on line 7.\nThis is on line 8.\n");

?>
</body>
</html>

produces (via View Source)

<html>
<body>
This is on line 1.\nThis is on line 2.\nThis is on line 3.
This is on line 4.
This is on line 5.\nThis is on line 6.\nThis is on line 7.<br />
This is on line 8.<br />
</body>
</html>

which renders to ...

This is on line 1.\nThis is on line 2.\nThis is on line 3. This is on line 4. This is on line 5.\nThis is on line 6.\nThis is on line 7.
This is on line 8.

Only line 8 is actually on a seperate line.

Regards,

Richard.
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 50 total points
Comment Utility
The answer here is if you want breaks in both the code and in the rendered output, you need ...


nl2br("\n");



Regards,

Richard.
0
 

Author Comment

by:huxun
Comment Utility
WTF!!!
0
 

Author Comment

by:huxun
Comment Utility
I'll give you the points, you deserve them. Don't spend them all at once.
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now