0

I would like to save some text to my database which can be entered by the client with PHP and MySQL. Lets say I have a textarea and I want to save the exact layout, how the client entered the text.

By exact layout I mean:

  • Exact spaces
  • Exact linebreaks

After that I want to display it to them in the same format. Of course any HTML JS tags will be wiped out when we're outputting the text.

Fragile
  • 71
  • 1
  • 6
  • I follow your question up until "Of course any HTML JS tags will be wiped out". That's not exact anything. Why not just use `
    `?
    – Brad Mar 21 '14 at 14:57
  • You can use the [nl2br()](http://www.php.net/manual/en/function.nl2br.php) function to deal with the linebreaks. – caRameL Mar 21 '14 at 14:57

2 Answers2

3

It's a good practice to store the original text in the database but render the filtered text.

You can change the linebreaks to HTML with nl2br() and use htmlentities() function to prevent XSS vulnerability.

va5ja
  • 473
  • 1
  • 3
  • 8
1

You could display the text in a HTML

<pre> 

Element.

Johann Bauer
  • 2,488
  • 2
  • 26
  • 41