(PHP 4, PHP 5, PHP 7, PHP 8) nl2br — Inserts HTML line breaks before all newlines in a string Descriptionnl2br(string $string, bool $use_xhtml = true): string ParametersstringThe input string. use_xhtmlWhether to use XHTML compatible line breaks or not. Return ValuesReturns the altered string. ExamplesExample #1 Using nl2br()
<?php The above example will output: Example #2 Generating valid HTML markup using the use_xhtml parameter
<?php The above example will output: Welcome<br> This is my HTML document Example #3 Various newline separators
<?php The above example will output: This<br /> is<br /> a<br /> string<br /> See Also
CGameProgrammer at gmail dot com ¶ 17 years ago It's important to remember that this function does NOT replace newlines with <br> tags. Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! This caused problems for me regarding a function I was writing -- I forgot the newlines were still being preserved. If you don't want newlines, do:
<?php
ngkongs at gmail dot com ¶ 15 years ago
to replace all linebreaks to <br />
<?php
works perfect for me N/A ¶ 13 years ago Here's a more simple one: <?php fquffio at live dot it ¶ 8 years ago
Starting from PHP 4.3.10 and PHP 5.0.2, this should be the most correct way to replace <br /> and <br> tags with newlines and carriage returns. You might also want to be "platform specific", and therefore this function might be of some help: aabaev ¶ 3 years ago double quotes !== single quotes php > echo nl2br('\r\n'); Anders Norrbring ¶ 16 years ago Seeing all these suggestions on a br2nl function, I can also see that neither would work with a sloppy written html line break.. Users can't be trusted to write good code, we know that, and mixing case isn't too uncommon. I think this little snippet would do most tricks, both XHTML style and HTML, even mixed case like <Br> <bR /> and even <br > or <br />. <?php gautier dot michelin at gmail dot com ¶ 2 years ago This is a simple trick to transform newlines to paragraph a $string : <?php You can bring it to a function : <?php Collette ¶ 2 years ago
Let's say a form text field does contain: 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 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 <br /> depending on the platform it runs: <?php Six" . PHP_EOL); ?> 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). nl2br2 ~ 0.0000309944153 s The function nl2br2: Justinas M. ¶ 6 years ago
This is example with "\R" regex token which matches any unicode newline character. <?phppublic function nl2br($string) Some PHP7 benchmarks: function nl2br_preg_R($string){ return preg_replace('/\R/u', '<br/>', $string); } function nl2br_preg_rnnr($string){ return preg_replace('/(\r\n|\n|\r)/', '<br/>', $string); }?> # nl2br ## Time: 0.02895712852478 s # nl2br_str # nl2br_preg_R # nl2br_preg_rnnr 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 <br /> tags added by nl2br(). So for others <?php filter_input(INPUT_POST, 'text', FILTER_SANITIZE_SPECIAL_CHARS); ?> returns newlines as and so nl2br will miss them. rather use <?php filter_input(INPUT_POST, 'text', FILTER_SANITIZE_STRING); ?> 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, <?php j dot mons54 at gmail dot com ¶ 10 years ago for bbcode :
<?php
hyponiq at gmail dot com ¶ 15 years ago On the contrary, <b>mark at dreamjunky.comno-spam</b>, 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. |