How to remove line breaks from a string?

How to remove line breaks from a string?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to remove line breaks from a string while inserting <br /> tags

When preparing user input for display in an HTML context, it is common practice to convert newline characters into visible line breaks using <br /> tags. In Laravel, the helper function nl2br() handles this by iterating through a string and injecting the tag at every occurrence of \n or \r\n. However, there is a specific scenario where this default behavior becomes problematic: when you need to strip the invisible newline characters entirely while still preserving the visual separation.

Consider the following input string:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!

Greetings,
Bill

Using nl2br() on this string results in output where the original newline characters remain embedded within the string, alongside the new HTML tags:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill

The issue here is that the underlying \n and \r characters are still present in the DOM node. While they might not render as visible whitespace on some platforms, they technically violate the requirement for a "one line" output where the newline entities themselves are removed from the data stream. The desired result looks like this:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br /><br />Greetings,<br />Bill

To achieve this, you need to preprocess the string to remove the newline characters before applying the conversion logic. The most robust approach involves using str_replace or a regular expression to strip the line breaks first, then apply nl2br() if necessary. However, a more efficient pattern is to replace the newlines directly with <br /> tags.

In Laravel, you can leverage the string helper methods to accomplish this without relying solely on nl2br(). The preg_replace function offers precise control over character substitution:

use Illuminate\Support\Str;

$string = "Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!

Greetings,\nBill";

// Remove existing line breaks and replace them with <br /> tags
$htmlString = Str::replace(['\n', "\r\n", "\r"], '<br />', $string);

This method explicitly targets the newline characters (\n, \r\n, and standalone \r) and substitutes them with the HTML equivalent. By doing so, you ensure that the resulting string contains no hidden line break characters, satisfying the requirement for a single continuous line of text in the DOM while maintaining the intended visual layout.

If you prefer to keep using nl2br(), you must explicitly strip the newlines first:

$htmlString = nl2br(Str::replace(['\n', "\r\n", "\r"], '', $string));

This two-step process ensures that nl2br() receives a string devoid of newline characters, effectively preventing it from adding tags at positions where no actual line breaks exist. It also prevents the accumulation of empty <br /> tags that might occur if multiple newlines are collapsed into one before processing.

There are trade-offs to consider depending on your specific use case. Using preg_replace with a single pattern like /[\r\n]+/ can be more concise:

$htmlString = Str::replace('/[\r\n]+/', '<br />', $string);

This regex matches one or more consecutive line breaks and replaces the entire block with a single <br />. This approach is useful if you want to collapse multiple empty lines into a single visual break. However, if your requirement is strictly to map every newline character individually without collapsing them, the simple replacement array used earlier is safer.

Another consideration is how browsers handle whitespace. Even after removing newlines, trailing spaces might remain if the original input had indentation. If you need to trim those as well, combine trim() or Str::trim() with your replacement logic:

$htmlString = Str::replace('/[\r\n]+/', '<br />', trim($string));

By understanding how nl2br() operates internally and having the ability to manipulate the string before conversion, you gain full control over the final HTML output. This level of precision is essential when rendering user-generated content where exact formatting and character cleanliness are paramount.

Our network