[PHP 4, PHP 5, PHP 7, PHP 8]
nl2br — Inserts HTML line breaks before all newlines in a string
Description
nl2br[string $string
, bool $use_xhtml
= true
]: string
Parameters
string
The input string.
use_xhtml
Whether to use XHTML compatible line breaks or not.
Return Values
Returns the altered string.
Examples
Example #1 Using nl2br[]
Collette ¶
2 years ago
Let's say a form text field does contain:
test line number 1
test line number 2
To remove all the line breaks and split the input into an array:
$input_from_text_field = preg_replace['#[\r\n]#','',nl2br[$input_from_text_field,false]];
$lines = explode["
",$input_from_text_field];
$lines will now be an array where $lines[0] = "test line number 1" and so on.
buzanits at gmail dot com ¶
4 years ago
If you write code that is to run in browser AND on the shell, this function could be useful. It uses PHP_EOL or
depending on the platform it runs:
leo dot mauro dot desenv at gmail dot com ¶
6 years ago
I test empirically this function nl2br and nl2br2 [create by ngkongs at gmail dot com].
Both work nice with different ASCII chars for linebreak, but the function nl2br2 is faster than nl2br.
nl2br2 ~ 0.0000309944153 s
nl2br ~ 0.0011141300201 s
The function nl2br2:
Justinas M. ¶
6 years ago
This is example with "\R" regex token which matches any unicode newline character.
"u" flag treate strings as UTF-16. Which is optional, depending on your use case.
NOTE:
preg_replace versions are much slower than using str_replace version or built-in nl2br.
Check out pcre.backtrack_limit php.ini setting for information about PCRE limit. It's good to know.
Some PHP7 benchmarks:
# nl2br
## Time: 0.02895712852478 s
# nl2br_str
## Time: 0.027923107147217 s
# nl2br_preg_R
## Time: 0.13350105285645 s
# nl2br_preg_rnnr
## Time: 0.14213299751282 s
chad at nobodyfamous dot ca ¶
5 years ago
I just spent a whole day trying to figure out why my textarea $_POST was not getting
tags added by nl2br[]. So for others
returns newlines as
and
so nl2br will miss them.
rather use and newlines will remain intact so nl2br will pick them up.
blacknine313 at gmail dot com ¶
14 years ago
After a recent post at the forums on Dev Shed, I noticed that it isn't mentioned, so I will mention it.
nl2br returns pure HTML, so it should be after PHP anti-HTML functions [ such as strip_tags and htmlspecialchars ].
darenschwenke at yahoo dot com ¶
8 years ago
This one works with br tags having attributes, in any case,
closed or not closed, and does not double linefeeds
I combine this with strip_tags[] for dead simple "contenteditable" fields allowing only text and linefeeds.
j dot mons54 at gmail dot com ¶
10 years ago
for bbcode :
hyponiq at gmail dot com ¶
15 years ago
On the contrary, mark at dreamjunky.comno-spam, this function is rightfully named. Allow me to explain. Although it does re-add the line break, it does so in an attempt to stay standards-compliant with the W3C recommendations for code format.
According to said recommendations, a new line character must follow a line break tag. In this situation, the new line is not removed, but a break tag is added for proper browser display where a paragraph isn't necessary or wanted.