php – &quot;\n&quot; be turned into &quot;<br> &quot; automatically

Question:

I'm developing a post system in PHP with MySQL. I need the line breaks that the user enters in the textarea be converted to <br /> .

The $texto variable (contains the post content) goes to the database in the conteudo column (type TEXT).

When the user breaks a line in the textarea , a space is automatically added. I needed to replace that space with a <br /> .

Answer:

nl2br

(PHP 4, PHP 5, PHP 7) nl2br — Insert HTML line breaks before all newlines in a string

Description

string nl2br ( string $string )

Returns string with <br /> inserted before all newlines.

Scroll to Top