php – Is it possible to keep the formatting of the text of a textarea when inserting it into MySQL and retrieving it?


I am making a website that has a news section. The page will have an administrative sector in which people (with almost no knowledge of computers) will write news.

The idea is that the news is formatted (by format I mean line breaks).

How can I do that? Basically I want my PHP code to understand each space as a breakline in the insert .


Perhaps I am not being very clear. The idea is that someone types something like:

Viacom, the fifth-largest media conglomerate in the world and with a market value of US$14.7 billion, also participates in the Paramount and DreamWorks studios, and has its own studio in the Buenos Aires neighborhood of Palermo.

The agreement is still pending the final signatures, warned local sources, in addition to requiring the approval of the National Communications Entity (Enacom). Already in early September, Viacom's number one in the region, Pierluigi Gazzolo, had met with Miguel de Godoy, head of the control body.

And that breakline looks the same in the SQL insert so that when it comes to putting the news on the web, it takes the corresponding format.


I don't know if this is exactly what you mean. Having a string stored like the following:

"Hola soy Juan \n tengo una bicicleta \n y me voy con mis amigos a jugar \n al futbol"

and a textarea with an id :

<textarea id="miTextArea" style=""></textarea>

you can use the property word-break: break-all; CSS to honor \n as line breaks.


document.getElementById("myTextarea").value = "Hola soy Juan \n tengo una bicicleta \n y me voy con mis amigos a jugar \n al futbol"
  font-size: 16px; 
  font-family: monospace; 
  height: 15em; 
  resize: none;
  word-break: break-all;
<textarea id="myTextarea" style=""></textarea>

EDIT: When inserting the string in the database, simply insert the text as users insert it in the textarea. It seems that MySQL does save the formatting of the text as far as line breaks are concerned.

Reference:Adding a line break in MySQL INSERT INTO text

Scroll to Top