Friday, February 15, 2013

Preserve Paragraphs from MySQL in PHP using nl2br()

Greetings of the Day!!!

When I was working with one of the website Designing, I noticed a strange problem. Maybe it's not a problem, but for a developer it looks like one. Many of us can come across such an issue, so I thought of penning down the known solution for it.

The problem

When you store a paragraph of text in a MySQL database (in a TEXT or VARCHAR column) and retrieve it with PHP, the text may display as a single continuous block in the browser with no paragraph breaks. The data itself still contains newline characters, but HTML ignores plain newlines and renders them as a single space.

Why this happens

HTML does not render raw newline characters ("\n" or "\r\n") as line breaks. When the browser receives plain text with newline characters, it collapses whitespace and shows the content as one paragraph.

The simple solution: nl2br()

PHP provides a built-in function nl2br() which converts newline characters to HTML line break elements (
or
). Use it when you want to display text from the database with the original line breaks preserved.

Example:

// Basic usage
echo nl2br($row_jobdetails['job_description']);

That will convert each newline in job_description into a
so the browser shows paragraph-like breaks.

Secure output (important)

If the stored text can contain user input, you should escape HTML to prevent cross-site scripting (XSS) before adding
tags. Combine htmlspecialchars() with nl2br():

// Secure usage: escape HTML, then convert newlines to <br>
echo nl2br(htmlspecialchars($row_jobdetails['job_description'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));

This prints user-entered text safely and preserves visible line breaks.

Alternatives

  • CSS approach: If you prefer not to alter the text on the server side, you can use CSS to preserve line breaks:
<div style="white-space: pre-line;">
  <!-- raw text with newlines will show line breaks here -->
  <?php echo htmlspecialchars($row_jobdetails['job_description'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
</div>

The CSS property white-space: pre-line preserves newline characters as line breaks while still wrapping long lines.

  • Storing HTML: If you deliberately store HTML in the database (for example, content entered in a WYSIWYG editor), you may already have

    or
    tags in your content. In that case, do not run htmlspecialchars() when outputting, but ensure input is sanitized on entry.

Notes about column types

Using TEXT or VARCHAR does not change how browsers render newlines. Newlines are a part of the stored string. What matters is how you convert or render those newline characters when generating HTML.

Example recap

// Unsafe if text is user-provided:
echo nl2br($row_jobdetails['job_description']);

// Safer for user-provided text:
echo nl2br(htmlspecialchars($row_jobdetails['job_description'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'));

That's it. It does all the trick. Happy Coding!!!

No comments: