nl2br – PHP String Functions

Syntax :

string nl2br( string , use_xhtml);

Description :

It is an inbuilt function of PHP. nl2br() function inserts HTML ( <br> or </ br> ) line breaks before all newlines in a string.


Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, It is the input string.

xhtml_use, Optional, Integer , It  decides Either XHTML compatible line breaks to be used or not. Only boolean values can used here. By default its set to TRUE and it will insert <br /> . We can also set it to FALSE and it will Inserts  <br>.

[/table]


Output :

It returns the altered string after adding HTML line breaks before all newlines.


ChangeLog :

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Version, Description
PHP 5.3.0 , Added the optional is_xhtml parameter. Before this version ‘<br />’ was always inserted.
[/table]


nl2br() – PHP Functions Example 1 :
<?php
echo nl2br("www.\ntutorialmines.\nnet");
?>


Output of above code in the browser is as below:

www.
tutorialmines.
net

HTML output of above code in the browser (View Source) is as below:

www.<br />
tutorialmines.<br />
net

nl2br() – PHP Functions Example 2 :
<?php
echo nl2br("www.\ntutorialmines.\nnet", false);
?>


Output of above code in the browser is as below:

www.
tutorialmines.
net

HTML output of above code in the browser (View Source) is as below:

www.<br />
tutorialmines.<br />
net


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *