Latest web development tutorials

PHP nl2br () function

PHP String Reference PHP String Reference

Examples

In a string of new line (\ n) inserts a line break before:

<?php
echo nl2br("One line.nAnother line.");
?>

Browser output of the code above is as follows:

One line.
Another line.

Enter the HTML code above is as follows (view source):

One line.<br />
Another line.

Running instance »

Definition and Usage

nl2br () function in the string of each new line (\ n) is inserted before the HTML line breaks (<br> or <br />).


grammar

nl2br( string,xhtml )

参数 描述
string 必需。规定要检查的字符串。
xhtml 可选。一个表示是否使用兼容 XHTML 换行的布尔值:
  • TRUE- 默认。插入 <br />
  • FALSE - 插入 <br>

technical details

return value: Returns the converted string.
PHP version: 4+
Update log: Prior to PHP 4.0.5, this function insert <br>. After PHP 4.0.5, this function into XHTML-compliant <br />.

In PHP 5.3, add the xhtml parameters.


More examples

Example 1

By using xhtml parameter, the new line (\ n) inserts a line break before:

<?php
echo nl2br("One line.nAnother line.",false);
?>

Browser output of the code above is as follows:

One line.
Another line.

Enter the HTML code above is as follows (view source):

One line.<br>
Another line.

Running instance »


PHP String Reference PHP String Reference