Is it possible to maintain the text format of a textarea by inserting it into MySQL and recovering it?

5

I'm 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 the line breaks).

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

Edit:

Maybe I'm not being very clear. The idea is that someone type something like:

  

Viacom, fifth worldwide media conglomerate with a market value of US $ 14,700 million, 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 from the latest signatures, warned local sources, in addition to requiring the approval of the National Communications Agency (Enacom). Already at the beginning of September the number one of Viacom in the region, Pierluigi Gazzolo, had met with Miguel de Godoy, head of the control body.

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

    
asked by Nick 04.11.2016 в 01:16
source

2 answers

2

I do not know if this is exactly what you mean. Having a stored string 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 a id :

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

You can use the property word-break: break-all; CSS to respect the \n as line break.

Example:

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

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

Reference: Adding a line break in MySQL INSERT INTO text

    
answered by 04.11.2016 в 01:26
0

With nl2br () you can get it see demo :

$cadena = "Esto\r\nes\n\runa\ncadena\r";
echo nl2br($cadena);

// Resultado:
// Esto<br />
// es<br />
// una<br />
// cadena<br />

If you receive the text string of the database and put it within a <textarea> you do not have to do anything, since it saves it with all the format and it is displayed as the user has entered it. Otherwise you have to use nl2br() .

Note security:

I recommend saving the user's texts, without transforming it in any way in the database.

Only and always when you receive the user's text from the database and you want to show it on your website, also use htmlspecialchars () .

  

Convert HTML entities again into special characters.

$string = htmlspecialchars($texto_de_la_base_de_datos);

<textarea><php echo $string; ?></textarea>

// ------ //

<p><php echo nl2br($string); ?></p>
    
answered by 04.11.2016 в 01:27