This question already has answers here:
Is a new line = \n OR \r\n?
(4 answers)
Closed 8 years ago.
echo("<li>Hello</li>\n"); seems to work fine for putting a new line in the HTML.
Is \r also required? For instance, echo("<li>Hello</li>\r\n");
Thanks
If you just want your HTML to show up as new lines in the source (or to look "pretty" as you stated in a comment you made) without having everything all clumped in single lines of code, concatenate the last lines of code using . "\n"
I.e.: echo("<li>Hello</li>" . "\n"); and echo "<table>" . "\n"; etc. that way you'll get nicely formatted and aligned HTML.
Using <br> will print <br> in the HTML source, while using \n will not.
For example:
Using:
echo("<li>Hello</li>" . "\n");
echo("<li>Hello again</li>" . "\n");
echo("<li>Hello to you too</li>" . "\n");
The HTML source will be:
<li>Hello</li>
<li>Hello again</li>
<li>Hello to you too</li>
As opposed to using <br>
echo("<li>Hello</li>" . "<br>");
echo("<li>Hello again</li>" . "<br>");
echo("<li>Hello to you too</li>" . "<br>");
The HTML source will be:
<li>Hello</li><br><li>Hello again</li><br><li>Hello to you too</li><br>
This makes it harder to go through HTML source in order to troubleshoot/debug etc.
That is only three lines of code; imagine having dozens or hundreds of lines?
Footnotes:
Using \r is perfectly fine for echoing and will not affect your code if added or omitted; however you're just using more characters than is required when wanting to get clean well-formatted and aligned HTML source.
Just \n will suffice; for echo'ed output.
Comments have already been given under your original question in regards to using \r for the purpose and relation to files under Windows and older versions of MAC, therefore I won't repeat myself.
The newline in this case is not because of the \n, but because of the <li>. HTML transforms Whitespace into a single space, unless you use something like <pre>.
No, it is never required. In fact the newline isn't required in HTML either.
Not required. additionally, list items will always be on new lines.
Use the PHP_EOL constant and PHP will use whatever is native on that platform.
See http://www.php.net/manual/en/reserved.constants.php
In HTML You have always to use
"<br />"
<?php echo "<div>HELLO</div><br />"; ?>
BUT, I had this problem to save a text on a file.
On windows just the \n to break line didn't work, when I opened the file in a text editor all the text was in the same row, so I had to use \n\r
Well, the various operating systems use different newline characters, but in practice browsers understand most of the newline conventions, i.e. \n, \r and \r\n.
3.7.1 Canonicalization and Text Defaults
[...]
When in canonical form, media subtypes of the "text" type use CRLF as
the text line break. HTTP relaxes this requirement and allows the
transport of text media with plain CR or LF alone representing a line
break when it is done consistently for an entire entity-body. HTTP
applications MUST accept CRLF, bare CR, and bare LF as being
representative of a line break in text media received via HTTP. [...]
Source: https://www.ietf.org/rfc/rfc2616.txt (emphasis by me). LF stands for "Line Feed" and is the \n character. CR stands for "Carriage Return" and is the \r character.
The problems with newline characters will only come up if you open the source code in some text editors. For instance, Linux newlines (\n) may cause the text to be displayed on one line in Notepad (which uses Windows' \r\n newline). This may not happen in newer version of Notepad, but it is just an example.
To force a new line use this:
echo("<li>Hello</li> <br />");
NOTE: A <li> element is by default a HTML block element, so a new line is generated automatically.
If you had a stylesheet with the following code
li {
display: inline; /*changes block-Element into an inline-Element*/
}
the <br /> would be needed to force a new line.
Click here if you want to know more about block and inline elements in HTML.
Related
if im not wrong \n representation means that a newline as <br> .But when i use <br> or another tags they work properly but escape sequences.
example
echo "write somethings<br>";
echo "about coding";
above example works fine but when i try to use escape sequences none of them are not working
echo "write something\n";
echo "about coding";
it's just an example for newline character and the other escaping characters dont work as \n.What is the real logic on this case?
\n and other similar escape sequences are not part of HTML. You should use HTML escape sequences. These can be found here: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
So only your <br> tag works but \n is not
No, this is an example of HTML rules.
Putting \n in a PHP string and then outputting it as HTML will put a new line character in the HTML source code. It's just line pressing return when writing raw HTML.
HTML puts no special meaning on the new line character (at least outside of script elements and elements with various non-default values of the CSS white-space property) and treats it like any other white space character.
<br>, on the other hand, is a line break element (but usually an indication that you should be using a block level element around the content instead).
HTML ignores carriage return and linefeed characters, treating them as whitespace. If you want to use display a string formatted with "\n" you can use nl2br to convert it, e.g.
echo nl2br("this is on\ntwo lines");
If you look at this in the browser it wont work : browser knows only HTML for display (<br>) but not escape like \n or \r
When I assign a variable (or even a string literal) containing several cr lf character pairs to an empty div or p element using a php echo statement the cr lf are gone replaced by a single space char (0x20) and so the new line formatting is lost. The text is displayed as one long line although the element does wrap it.
It seemed that the cr lf was gone by looking at the displayed text. I verified this by copy-pasting the displayed text into a hex editor. How do I tell php to retain the cr lf characters in the text file? Here's the assignment code:
echo "<div class='showBox'><p> $content </p></div>";
This has the same results with or without the enclosing p element.
After trying many schemes by chance I discovered a built in php function that produces the exact output format I was hoping for. By executing . .
$content = nl2br($content);
. . before echoing the html to the browser . .
echo "<div class='showBox'><p class='dispContent'>$content></p></div>";
. . the displayed output now retains the blank lines the user entered with the (ENTER) key.
Thanks to both commenters for the valuable hints which led me to this useful discovery.
I am making a comments system in which i can accept user input with line breaks.
I don't want to show the \n or \r thing
Please help me with this
nl2br($string);
is fast and easy
They are enabled by default. If you are outputting the text to a web browser, make sure to use nl2br or the white-space attribute in CSS.
using preg_replace
simply replace it
preg_replace('/\n/'," ",$str);
\n should do the trick.
if you are trying to output a textarea, then use nl2br();
also:-
If you are trying to format your HTML source, you should use the constant PHP_EOL. The main reason being that on windows machines the EOL is \r\n and on UNIX machines it is \n. With a properly installed LAMP set up just use PHP_EOL like so.
$html.="<p>This is my HTML</p>" . PHP_EOL;
Line breaks will be stored just like any other character.
\n is an escape code that allows you to explicitly insert a line break into a string, but I don't think that it's relevant here.
The issue you're actually facing is that HTML does not impart any visual meaning to a line break. Line breaks within HTML code do not, under normal circumstances, equate to a line break on the screen.
One way to render a line break in HTML is to use a line break tag, or <br>.
In PHP, you can automatically convert line breaks to <br> with the nl2br function. Applying this to your comment text when you output it into HTML will enable you and other visitors to see the line break visually.
I have a php parser that split a given string by line-breaks, doing something like this:
$lines = explode(PHP_EOL,$content);
The parser works fine when working on server side. However, when I pass the content via post by ajax (using jquery's $.post method) the problem arises: line breaks are not recogniezed. So after almost an hour of tests and head-aches I decided to changed PHP_EOL by "\n" and it worked:
$lines = explode("\n",$content);
Now it works! Damn it I lost so much time! Could somebody explain me when use PHP_EOL and "\n" properly, so I can save time in the future? Appreciate your kind answers ;)
The constant PHP_EOL should generally be used for platform-specific output.
Mostly for file output really.
Actually the file functions already transform \n ←→ \r\n on Windows systems unless used in fopen(…, "wb") binary mode.
For file input you should prefer \n however. While most network protocols (HTTP) are supposed to use \r\n, that's not guaranteed.
Therefore it's best to break up on \n and remove any optional \r manually:
$lines = array_map("rtrim", explode("\n", $content));
Or use the file(…, FILE_IGNORE_NEW_LINES) function right away, to leave EOL handling to PHP or auto_detect_line_endings.
A more robust and terser alternative is using preg_split() and a regexp:
$lines = preg_split("/\R/", $content);
The \R placeholder detects any combination of \r + \n. So would be safest, and even work for Classic MacOS ≤ 9 text files (rarely seen in practice).
Obligatory microoptimization note:
While regex has a cost, it's surprisingly often speedier than manual loops and string postprocessing in PHP.
And there are a few classic examples where you should avoid PHP_EOL due to its platform-ambiguity:
Manual generation of network protocol payloads, such as HTTP over fsockopen().
For mail() and MIME construction (which really, you shouldn't do tediously yourself anyway).
File output, if you want to consistently write just Unix \n newlines regardless of environment.
So use a literal "\r\n" combination when not writing to files, but preparing data for a specific context that expects network linebreaks.
PHP_EOL should be used when writing output such as log files.
It will produce the line break specific to your platform.
PHP_EOL is a constant holding the line break character(s) used by the server platform. In the case of Windows, it's \r\n. On *nix, it's \n. You apparently have a Windows server.
If you were on a *nix server, that change wouldn't have fixed it, because it would be \n. If you are sending data to the client (i.e. the browser), you should use \r\n to ensure line breaks are recognized.
PHP_EOL is the line ending used by the server PHP is running on. User submitted content will probably have line ending in whatever format they use. However, instead of exploding on newlines, just using the file() function, it does exactly what you are after.
IMHO using PHP_EOL is preferable
to ensure consistency between PHP and JS handling of line break, you may want to define end-of-line variable in JS using PHP_EOL
var eol = '<?php echo str_replace(array("\n","\r"),array('\\n','\\r'),PHP_EOL) ?>';
afterwards, use eol for splitting submitted textarea content
when a carriage return follows a closing php tag, php doesn't print it.
How can I change this?
Thanks a lot
That's normal behavior, and cannot be changed : the newline after a closing ?> is always ignored.
Here's the reference, in the FAQ of the PHP manual : Hey, what happened to my newlines?
(quoting, emphasis mine)
<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>
In PHP, the ending for a block of code
is either "?>" or "?>\n" (where \n
means a newline). So in the example
above, the echoed sentences will be on
one line, because PHP omits the
newlines after the block ending. This
means that you need to insert an extra
newline after each block of PHP code
to make it print out one newline.
Why does PHP do this? Because
when formatting normal HTML, this
usually makes your life easier because
you don't want that newline, but you'd
have to create extremely long lines or
otherwise make the raw page source
unreadable to achieve that effect.
And here are a couple of interesting reads about this :
Rules pertaining to HTML or whitespace preceding or following PHP tags
PHP Stripping Newlines
The history of PHP eating newlines after the closing tag -- goes back to PHP 3 ^^
It's a default behavior of the language.
If you need the line break, you put a echo "\n" or echo "<br>" as the last line of the script.
This is intended behavior (see Escaping from HTML):
[…] when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) […]