Converting <br /> into a new line for use in a text area

Converting <br /> into a new line for use in a text area

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

Stefan Bogdanescu

Founder & Senior Architect

Converting <br /> into Newlines for Text Area Display

When rendering a string containing HTML tags like <br /> directly inside a <textarea> element, the browser treats the content as raw text rather than rendered HTML. Consequently, the user sees the literal characters <, b, r, /, >, instead of an actual line break. To fix this, you need to replace the tag sequence with a newline character (\n) before assigning the value to the input field.

In your specific scenario, where the session variable $var1 contains "Line 1 info blah blah <br /> Line 2 info blah blah", simply echoing that string into the textarea will persist the raw tags. The solution lies in preprocessing the string on the server side using str_replace.

Consider the following implementation:

$convert = $_SESSION["hobbieTalk"];
$convert = str_replace("<br />", "\n", $convert);
echo $convert;

This logic replaces every occurrence of the <br /> tag with a newline character. When this processed string is placed inside the PHP block within your <textarea>, the browser receives the actual line breaks rather than the HTML markup.

However, there is a critical nuance regarding how different browsers handle line endings. While \n works on most modern systems, some environments might expect \r\n. If you encounter issues where lines still appear concatenated despite the replacement, try using str_replace("<br />", "\r\n", $convert) instead. This ensures cross-browser compatibility by adhering to the CRLF standard often used in Windows-based text files.

Handling Multiple Variations of Line Breaks

Your current code assumes that every line break is explicitly marked with <br />. In practice, HTML content can be inconsistent. It might contain <br>, &nbsp; followed by a space, or even actual newline characters already present in the source string. To ensure robustness, you should handle these variations simultaneously:

$convert = str_replace(['<br>', '<br />', '&nbsp;', ''], "\n", $convert);

This chained replacement removes extra whitespace that often accompanies HTML tags and normalizes the output. Note that replacing &nbsp; with a newline is generally unnecessary unless you are specifically cleaning up broken HTML where non-breaking spaces were used to simulate indentation or spacing. A safer approach might be to remove them entirely if they represent unwanted gaps, keeping only the structural line breaks.

The Trap of Double-Encoding

A common pitfall when working with text areas is confusing HTML encoding with string escaping. If your variable $var1 was originally retrieved from a database where special characters were already escaped (e.g., < became &lt;), you must ensure you are replacing the correct entity sequence.

If your session data literally contains the string &lt;br /&gt;, your current str_replace("<br />", ...) will fail because it is looking for the unencoded characters. In that case, you need to replace the encoded version:

$convert = str_replace("&lt;br /&gt;", "\n", $convert);

Always inspect the raw value of $convert using var_dump($convert) during development to determine whether your data is already decoded or still contains HTML entities. This diagnostic step prevents silent failures where the replacement logic simply does nothing because the target string doesn't match.

Security Considerations

While replacing <br /> solves the display issue, be cautious about mixing raw user input with dynamic content. If this text area is used to store data that will later be rendered as HTML elsewhere in your application, ensure you are not introducing XSS vulnerabilities by blindly accepting HTML tags from untrusted sources. In a Laravel context, if you are storing this in a session or database, remember that htmlspecialchars should be applied when outputting the data back to the view, unless you are explicitly sanitizing it for storage as plain text.

For more complex scenarios involving rich text editing or sanitization, consider leveraging existing packages like mews/purifier. These tools handle the removal of dangerous tags while preserving formatting, reducing the risk of security breaches compared to manual string replacements.

Our network