the_archer the_archer - 1 year ago 93
SQL Question

Preserve Line Breaks From TextArea When Writing To MySQL

I'm using a textarea to enable users to input comments. However, if the users enters new lines, the new lines don't appear when they are outputted. Is there any way to make the line breaks stay.

Any idea how do preserve the line breaks?

Answer Source

Two solutions for this:

  1. PHP function nl2br():


    echo nl2br("This\r\nis\n\ra\nstring\r");
    // will output
    This<br />
    is<br />
    a<br />
    string<br />
  2. Wrap the input in <pre></pre> tags.

    See: W3C Wiki - HTML/Elements/pre