I've just been reading a solved thread with this same title. The solution was nl2br. I was unaware of this function and have been using str_replace instead.

A problem arises however, when converting preformatted text, specifically a table. Either solution leaves a mass of space between text and a table due to all the new lines generated.

Is there a solution to this.

Dani AI

Generated

This is the classic issue: converting every newline into a break tag will also turn the newlines that sit next to or inside HTML (for example a stored table) into visible blank lines. is right that trim only fixes leading/trailing whitespace; it does not collapse internal blank lines. 's one-line-table fix works, but it is brittle and hard to maintain.

Three practical approaches (ranked by robustness):

  • Fast and safe: skip newline-to-HTML conversion when the content already contains HTML. If your database row includes tags (for example <table>), treat it as HTML and do not run the newline-to-<br> pass. This is simple and avoids mangling intended markup.
// simple detection: if it looks like HTML, do not convert newlines
if (strpos($text, '<') !== false) {
    $output = $text; // already HTML
} else {
    $output = nl2br($text);
}
  • Quick cleanup for mixed input: remove newlines that occur only between tags and collapse multiple blank lines before converting the remainder. That reduces stray <br>s next to block elements.
// remove line breaks that sit between tags, then collapse repeated blank lines
$html = preg_replace('/>\s*\r?\n\s*</', '><', $html);
$html = preg_replace('/(\r?\n){2,}/', "\n\n", $html);
$html = nl2br($html);
  • Robust/production: parse the string with DOM (DOMDocument/DOMXPath), iterate text nodes, and convert newlines to actual <br> elements only for nodes whose parent is not pre, code, table, etc. This avoids touching markup and is the safest for real HTML. See the PHP DOM documentation for details: DOM extension manual. For the simpler fixes above, see the preg_replace notes: preg_replace manual.

Caveats: regex fixes work for many simple cases but can break complex HTML; the DOM approach is longer to code but is reliable. If users sometimes submit HTML and sometimes plain text, store a flag (or detect HTML) so you only apply newline conversion to true plain text.

Recommended Answers

All 2 Replies

Thanks MitkOK, you've done it again.
My problem arose because I only have a text area for input. Consequently I formatted the output from my database with the str_replace equivalent of nl2br.
However, I already had a preformatted article in the database which contained a table. I got around the problem by updating the database, so that the table code was all on the same line.

I assume I'd have to use the trim function only on table code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.