Gen. partnerAlgotech

PHP function nl2br()

Converts broken lines in text to html tags. This tag is used in places where the user enters any text (textarea) and there is a risk of using multiple lines of text.

|
February 16, 2020

This function converts line breaks (\n) in a string to the HTML tag <br>.

Parameters

Parameter Data type Default value Note
$string string not Input string.
$is_xhtml bool null Switches the escaping method according to the context.

Detailed description

$retezec = 'text
next text
and something else';
echo nl2br($retezec);

Returns:

text<br>
more text<br>
and something else

Converts broken lines in text to html tags. This tag is used in places where the user enters any text (textarea) and there is a risk of using multiple lines of text.

The treatment of classic inputs (type="text") is meaningless, as multi-line text cannot be entered here.

Note: As of PHP 4.0.5, the nl2br() XHTML function is eligible. All versions before 4.0.5 will return a string with a tag inserted before the line breaks instead of <br />.

Example

echo nl2br("Welcome\r\nThis is my HTML document", false);

Returns:

Welcome<br>
This is my HTML document

Return values

string

Returns an edited string including HTML tags.

Changes in versions

Version Note
5.3.0 Added optional is_xhtml parameter.
4.0.5 nl2br () is now XHTML compatible. All earlier versions will return a string with line breaks inserted instead of <br />.

Other resources

[Official nl2br function documentation](Official nl2br() documentation)

Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.