Solved

\n problem

Posted on 2002-07-11
9
191 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
ID: 7145989
Multiple post ... please delete!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7146105
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
ID: 7146266
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:huxun
ID: 7146282
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
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7146309
<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:
Richard Quadling earned 50 total points
ID: 7146317
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
ID: 7146322
WTF!!!
0
 

Author Comment

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

Expert Comment

by:Richard Quadling
ID: 7146337
Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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