Re: newline problem
| From: | Justin French | Date: | Thu, 25 Jul 2002 05:14:21 +0000 |
| Subject: | Re: newline problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-109423@lists.php.net to get a copy of this message | ||
A new line is NOT a <br> or <br /> in HTML.
Assuming this output is for HTML (browser) purposes, the browser will ignore
newlines.
<?php
echo 'this is first line<br>second line<br>third line'; // works for HTML
?>
Will print...
this is first line
second line
third line
... to the browser.
If you have a $string with \n's in it, you can convert these to <br />'s
with the function nl2br():
<?
// with newlines typed in, will work in email and text files:
$string = 'this is first line\nsecond line\nthird line';
// convert \n's to <br>'s for HTML output
$string_html = nl2br($string);
// newlines done with hitting return will work in email and txt files
$string = 'this is first line
second line
third line';
// convert \n's to <br>'s for HTML output
$string_html = nl2br($string);
?>
Think about it -- this HTML...
<P>
something
something
something
</P>
... will output somethingsomethingsomething to the browser, because browsers
ignore newlines, tabs, and other whitespace in text. On the other hand,
this HTML...
<P>
something<br>
something<br>
something
</P>
... would achieve three separate lines, so you need to do the same thing
when echoing.
HTH
Justin French
on 25/07/02 2:11 PM, Wormy . (w_lsy@hotmail.com) wrote:
> Dear all,
> this sounds like a silly problem but i really dunno how to fix it!
>
> In the php manual on php.net said we can use an embedded newline just
> by writing the string on different line by pressing "enter" key. This
> doesn't work for me. Another thing is \n also work. For example:
>
> <?php
> echo ' You can also have embedded newlines in strings,
> like this way.';
> echo 'I am trying to include at this point: \n a newline';
> ?>
>
> the output i get is:
> "You can also have embedded newlines in strings, like this way.I am trying
> to include at this point: \n a newline"
>
> Am I doing anything wrong? Thanks!
>
> Ser Yee
>
> _________________________________________________________________
> Join the worlds largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>