I'm trying to remove the
in output from database but this isn't really working out for me. My code starts with nl2br
, which produces
. But I will allow my users to edit the texts and I would love to make sure that it line breaks are allowed in editing too.
But in my it prints out the
tag. All I want is a line break.
echo nl2br[preg_replace["//", "", $row["content"]]];
Huey
5,0046 gold badges33 silver badges44 bronze badges
asked Nov 30, 2015 at 17:24
4
Don't run the nl2br
that is outputting br
s. Also use \n
in the replace value so you get new lines.
echo preg_replace["//", "\n", $row["content"]];
You also might want to use \h
or \s
instead of \W
.
answered Nov 30, 2015 at 17:29
As I understand, there is two forms that you would like to use, when you want to display the text entered in a textarea, then you need the nl2br, which inserts br tags into the text. You may also want to save the contents to a database this way.
When you want to load the text back to a textarea, then depending on how you saved the text:
- if it is saved with newlines: do nothing with the text just load it into the textarea
- if it is saved with br tags: you need the preg_replace[...] part of your code with '\n' as the replacement [
preg_replace["//", "\n", $row["content"]];
].
answered Nov 30, 2015 at 17:37
piftapifta
1866 bronze badges
[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 ¶
7 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.
# nl2br_str # nl2br_preg_RSome PHP7 benchmarks:
# nl2br
## Time: 0.02895712852478 s
## Time: 0.027923107147217 s
## 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.