nl2br
Insert HTML newline tag before all new lines of a string
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.
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.