Current Location: Home> Function Categories> nl2br

nl2br

Insert HTML newline tag before all new lines of a string
Name:nl2br
Category:String
Programming Language:php
One-line Description:Insert HTML newlines before each new line in the string.

Example

Example 1

Insert a newline before a new line (\n) in a string:

 <span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">echo</span> <span class="token function">nl2br</span> <span class="token punctuation">(</span> <span class="token string double-quoted-string">"One line.\nAnother line."</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span>
<span class="token delimiter important">?></span></span>

The browser output of the above code:

 One line.
Another line.

HTML input of the above code (see source code):

 One line.<br />
Another line.

Try it yourself

Example 2

By using the xhtml parameter, insert a newline before the new line (\n):

 <span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">echo</span> <span class="token function">nl2br</span> <span class="token punctuation">(</span> <span class="token string double-quoted-string">"One line.\nAnother line."</span> <span class="token punctuation">,</span> <span class="token constant boolean">false</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span>
<span class="token delimiter important">?></span></span>

The browser output of the above code:

 One line.
Another line.

HTML input of the above code (see source code):

 One line.<br>
Another line.

Try it yourself

Similar Functions