Instance
Insert a newline character before the new line (\ n) in the string:
<?phpecho nl2br ("One line.\nanother line.");? >
The browser output of the above code is as follows:
One line. Another line.
The HTML input for the above code is as follows (see source code):
One line.<br/>another line.
Definition and usage
The NL2BR () function inserts an HTML line break (<br> or <br/>) before each new line (\ n) in the string.
Grammar
NL2BR (string,xhtml)
Parameter description
string is required. Specifies the string to check.
XHTML is optional. A Boolean value that indicates whether to use compatible XHTML wrapping:
true-default. inserting <br/>
FALSE-Insert <br>
Technical details
Return value: Returns the converted string.
PHP version: 4+
Update log: Before PHP 4.0.5, this function inserts <br>. After PHP 4.0.5, this function inserts <BR/> compatible with XHTML.
In PHP 5.3, a new XHTML parameter was added.
More examples
Example 1
Insert a newline character before the new line (\ n) by using the XHTML parameter:
<?phpecho nl2br ("One line.\nanother line.", false);? >
The browser output of the above code is as follows:
One line. Another line.
The HTML input for the above code is as follows (see source code):
One line.<br>another line.
A simple example:
<?php$str = "Welcome to Www.nowamagic.net"; Echo nl2br ($STR);? >
HTML code to run the result:
Welcome to <br/> www.nowamagic.net