Home >>PHP String Functions >PHP nl2br() Function
PHP nl2br() function is used to insert HTML break tags <br> in the place of all new lines present in a given input string. Break tg is used to break the statement and go to the next line. This function returns a string with <br> or <br /> before all the newlines like \r or \n in the input string.
Syntax:-
nl2br($string, $xhtml);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the given input string. |
xhtml | This is an optional parameter.This parameter contains a boolean value that indicates whether or not to use XHTML compatible line breaks:
|
Here is an example of nl2br() function in PHP:
<html> <body> <?php echo nl2br("Hello World.\nPHPTPOINT."); ?> </body> </html>
HTML view of the above code:
<html> <body> Hello World.<br />PHPTPOINT. </body> </html>
Here is another example of nl2br() function in PHP using the xhtml parameter:
<html> <body> <?php echo nl2br("Hello World.\nPHPTPOINT.",false); ?> </body> </html>
HTML view of the above code:
<html> <body> Hello World.<br>PHPTPOINT. </body> </html>