Re: escape \n
| From: | Ashley Sheridan | Date: | Fri, 23 Apr 2010 08:05:18 +0000 |
| Subject: | Re: escape \n | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304457@lists.php.net to get a copy of this message | ||
On Fri, 2010-04-23 at 09:51 +0200, Nick Balestra wrote:
> Hello guys i am trying to figure out what is worng with thoose special escaped character, like
> \n \t \r ...
>
> As i cannot make them working. The browser doesn't display them, but doesn't eithr
> crate a new line, or else.
> I am using them fro example like this:
>
> print: "this shoudl be on a line \nwhile this on a new line";
>
> I've searched google and saw man people struggling with this, but apparently not a clear
> answer to why....maybe is a stupid beginner question, but i would just like to know. (Personally i
> solved for the moment by printing out <br> or <pre>, but i would like to understand
> this.
>
> Cheers, Nick
By default, PHP sends out HTML headers. Browsers ignore extraneous
white-space characters, and also new lines, carriage returns and tabs,
converting them all to a single space character.
If you view the source in your browser, you'll see the newlines, but in
regular display, your text is treated as HTML.
There is a function in PHP called nl2br, which accepts a string and
returns the same one with all the newlines replaced with <br>
automatically, which might be easier to use if your content is in a
string. Otherwise, the only way to get new lines on your actual page is
to either manually use <br> tags, put the text inside a <pre> block, or
use CSS to preserve the white-space.
Thanks,
Ash
http://www.ashleysheridan.co.uk