Example
Insert a newline character before a new line (\n) in the string:
<?php echo nl2br("One line.\nAnother line."); ?>
The browser output of the above code is as follows:
One line. Another line.
The above code The HTML input is as follows (view source code):
One line.<br /> Another line.
Definition and usage
nl2br() function inserts an HTML newline character (<) before each new line (\n) in the string ;br> or
).
Syntax
nl2br(string,xhtml)
Parameter Description
string Required. Specifies the string to check.
xhtml Optional. One indicates whether to use the Boolean value compatible with XHTML:
True-default. INSERT
FALSE - INSERT
Technical Details
##Return value: Return the converted string. PHP Version: 4+##Change Log: Before PHP 4.0.5 , this function inserts
. As of PHP 4.0.5, this function inserts an XHTML-compatible
.
In PHP 5.3, a new xhtml parameter was added.
More examples
Example 1
Insert a newline character before a new line (\n) by using the xhtml parameter:
<?php echo nl2br("One line.\nAnother line.",false); ?>
The browser output of the above code is as follows:
One line. Another line.
The HTML input of the above code is as follows (view source code):
One line.<br> Another line.
A simple example:
<?php $str = "Welcome to www.nowamagic.net"; echo nl2br($str); ?>
Run Resulting HTML code:
Welcome to <br /> www.nowamagic.net
The above is the detailed content of php function nl2br() that inserts html newline characters before each line in the string. For more information, please follow other related articles on the PHP Chinese website!