Page 1 of 1

Can't get \n or \r command to work

Posted: Thu Feb 12, 2009 6:03 pm
by RConf
Sorry if this one's a waste of time, I've done a search for a similar question - I'm a complete novice and I've just finished configuring my first WAMP test site. Working through a tutorial, I thought I had covered all the general php.ini setup issues, however when running simple test code:

<?php

// sample text here

$output = "This is one line.\n And this is another line.";
echo $output;

?>

...returns this in the browser (next line and carriage return characters don't work):

This is one line. And this is another line.

Have I missed something in my configuration?

Re: Can't get \n or \r command to work

Posted: Thu Feb 12, 2009 6:08 pm
by John Cartwright

Code: Select all

$output = "This is one line.\n And this is another line.";
echo nl2br($output);
:)

Re: Can't get \n or \r command to work

Posted: Thu Feb 12, 2009 6:10 pm
by Eran
This is not a PHP issue. Your browser reads output as HTML, which does not respect newlines (those are used for source code formatting) but only line-break tags (<br />).

You can convert the string to line-breaks using ln2br()

Code: Select all

$output = "This is one line.\n And this is another line."; 
echo nl2br($output);
Or use the white-space CSS attributre (pre-wrap).

http://www.php.net/nl2br
http://developer.mozilla.org/en/CSS/white-space

Re: Can't get \n or \r command to work

Posted: Thu Feb 12, 2009 8:23 pm
by RConf
I have much to learn.

Many thanks.