php Newlines vs html break - php

This code:
<?php
echo "This is the first line. \n";
echo "This is the second line.";
?>
Echoes out all on the same line: ("This is the first line. This is the second line.")
Shouldn't \n have basically the same function as <br> ? What am I missing here?

HTML doesn't render \n, a \n will put a break in your HTML source code
PHP
<?php
echo "<p>This is the first line. \n";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.
This is the second line.</p>
HTML
This is the first line. This is the second line.
PHP
<?php
echo "<p>This is the first line.";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.This is the second line.</p>
HTML
This is the first line.This is the second line.
PHP
<?php
echo "<p>This is the first line. <br/>";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.<br/>This is the second line.</p>
HTML
This is the first line.
This is the second line.

You mix up the output of php and the result in your browser. \n is newline. You may see it, when you read the source code in your browser(Ctrl + U in Chrome) .
But browser only render <br> as newline on webpage

echo "This is the first line. \n";
Will produce a linebreak in your source, meaning the cursor is currently on the next line (the line belove the text).
echo "This is the second line.";
Will produce a single line and leave the cursor right after the text (on the same line).
echo "This is the second line.<br />";
Will produce a single line but in rendered html containing a visible linebreak. However, in the sourcecode there will be no linebreaks, so:
echo "Line one<br />Line two";
Will render two lines in html but one line in the source.

echo "This is the first line.", '<br />', PHP_EOL;
^ HTML code for BR and ENTER.ENTER is visible in the source or PRE tags, or TEXTAREAs, or when enabled by CSS white-space (etc.) while BR is the line break in HTML.

Related

PHP - Split string containing newline and print

Why is the following code not working...?
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
}
It prints
hello world !
Instead of
hello
world
!
Thanks
You also have to echo <br />
The HTML <br /> element produces a line break in text (carriage-return).
It is useful for writing a poem or an address, where the division of
lines is significant.
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
echo "<br />";
}
doc: <br />
Another option is to use nl2br to inserts HTML line breaks before all newlines in a string
$test = "hello \n world \n !";
echo nl2br($test);
Doc: nl2br()
Add new line to Browser Output via HTML
In HTML, you need to add line break for new line via HTML Tag. This will not appear as required output if there is line break in text/string.
<br> used to add single line break.
Replace
echo $line;
Into
echo $line."<br>";
Add new line to string/source code
If you need to add new line into source code only then replace echo $line; with echo $line."\n";
\n is used into double quotes for new line.
Add new line to Browser Output via PHP
Anyways an other way to do this via PHP is to use PHP's built-in method nl2br()
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
}
The result is:
hello world !
Basically you have split on the newlines, and the newlines are not included as you print out each split part.
Use nl2br($line) and replace the delimiter with empty space.This way your new line will be visible in html:
$test = "hello \n world \n !";
foreach(explode(" ",$test) as $line){
echo nl2br($line);
}
will output:
hello
world
!
and the output html:
hello<br />
world<br />
!

Multiline block showing as one line PHP

I'm just running some example PHP code verbatim, but it's outputting as a single line in my browser. I'm expecting to see new multiple lines.
<?php
$author = "Alfred E Newman";
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>
Your browser by default assumes that any output is HTML and when displaying HTML, newline characters are treated like spaces. You'd either need to output HTML with BR or P tags to force newlines or you can send a content-type header to tell the browser that the output you are sending is plain text.
<?php
$author = "Alfred E Newman";
// tell the browser that your output is plain text
header("Content-Type: text/plain");
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>
<?php
$author = "Alfred E Newman";
$str = "This is a Headline
This is the first line.
This is the second.
- Written by $author.
";
echo nl2br($str);
?>
will give you what you need;

text-indent on user formatted text

I would like to use the text-indent property (or something like this) to add a indentation of the first line of each paragraph.
First the user can write his text in a textarea, then save it in a DB.
When I want to display this text i use :
$exhib = $res->fetch_array();
echo "<div class='infoContent'>". nl2br($exhib['description']) . "</p></div>";
The line return of the user are stored as \n in DB, and modified to <br /> by nl2br. With my CSS :
.infoContent
{
text-indent: 10px;
}
only the first line is indented. (normal behavior).
Q : How can I make this indentation automatic for each line after a <br /> tag ?
I tried a ugly solution, but it doesn't work because empty paragraph section <p></p> doesn't create another line return (in case the user enter 2 line return \n\n).
echo "<div class='infoContent'><p>" . str_replace("<br />", "</p><p>", nl2br($exhib['description'])) . "</p></div>";
I can replace <p></p> tag by <br /> but it seems to be a very bad solution...
EDIT:
JSfiddle
Thanks
\n\n usually means a new paragraph (enter). The white space between paragraphs is CSS and is actually default browser styling (1em I think?). \n is a <br> (shift + enter).
So don't use nl2br() and do it yourself:
$text = '<p>' . htmlspecialchars($text) . '</p>'; // HTML ENCODE!
$text = preg_replace('#\n\n\n*#', '</p><p>', $text); // 2 or more \n
$text = preg_replace('#\n#', '<br />', $text); // all left-over \n
$text = preg_replace('#><#', ">\n<", $text); // if you like </p>\n<p> with a newline between, like I do
http://3v4l.org/b0AhL
This is pretty much what Markdown does (and Textile and those): 1 newline = BR (not exactly in Markdown) and 2 newlines = P. I always use simple Markdown for rendering plain text.
When you submit your textarea, instead of using CSS to indent only the first line, you can use (non-breaking space).
when you submit your text area, I assume you grab it as such:
$userText = $_POST['description']
Well, before you submit to your database, you could use a simple replace - After you grab the text:
$userText = str_replace("\n", "\n ", $userText);
Then submit that to the database. When it comes back, the nl2br will still make the \n into a <br /> and then it won't see the , though the HTML will see them as four spaces (equal to an indent).
It's dirty, but simple!
Reference: http://www.w3schools.com/php/func_string_str_replace.asp

Unable to use metacharacters in php

i'm using metacharacter in a simple php code, but it is now showing Second line of text into a new line.
echo "First line of text \r\n Second line of text";
I know Unix based systems use just a "\n". But as i'm on Windows 7, so i am using "/r/n"
You can use :
echo nl2br("First line of text\n Second line of text");
nl2br function
Or you can use EOL in php
Try using other way with basic HTML tag <br />:
echo "First line of text" . "<br />" . "Second line of text";

Line breaks from textarea and mysql dont appear in html

How can I make text appear on diff lines in html?! They appear on diff lines in my text area when output from mysql, and also appear on diff lines inside mysql. But in the web page its all on one line. How can this be solved?
Use:
string nl2br ( string $string [, bool $is_xhtml = true ] )
It can handle \n, \r, \r\n or \n\r linebreaks and replaces them with a <br />
Example:
$yourText = "This is line one.\nThis is line two.";
$yourText = nl2br($yourText);
echo $yourText;
This will result in:
This is line one.<br />This is line two.
Link to the manual for more information.
Use the PHP nl2br function:
$string = "hello \n world";
$string = nl2br($string);
It's quite self-explanitory: \n gets replaced with <br />
Replace Linebreaks with <br>
str_replace(.N, '<br>', [element]);
You can use <pre>..Your text..</pre> tag for that.

Categories