Replacing \r\n with PHP

Replacing \r\n with PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Trap of Literal Carriage Returns in PHP Form Handling

When you submit a form containing line breaks, your browser automatically encodes those visual gaps into carriage return and newline characters (\r\n or \n). If you intend to store plain text in a database without these artificial line breaks, simply running str_replace on the string literal "\r\n" often fails. The issue isn't that your regex patterns are wrong; it's that the data entering your PHP script is already interpreted as actual byte sequences, not escaped strings.

In your specific case, you are attempting to strip characters using a series of str_replace calls with various escape combinations like '\\r\\n' or '/\r\\n/'. This approach misunderstands how PHP handles string literals versus binary data. When you write '\r\n' in PHP code, the parser immediately converts that into a two-byte sequence: a carriage return (0x0D) and a line feed (0x0A). You are not storing the text representation \r\n; you are storing the actual bytes representing a newline. Consequently, trying to replace '\r\n' with an empty string is the correct logical step, but your code snippets suggest you might be struggling with escaping the backslashes in the source code versus the content itself.

The core problem lies in how $_POST['text'] is handled before sanitization. If your input contains standard Windows-style line endings (\r\n), a single str_replace call with the correct literal newline characters will remove them. However, if you are seeing \r\n persist in your database entries as visible text (meaning the bytes 0x5C, 0x72, 0x5C, 0x6E are present), then the input was never converted to actual newlines by the browser or PHP. This usually happens if the data is being read from a file or an API where the encoding wasn't normalized, rather than a standard HTTP POST form.

To fix this reliably, stop trying to guess which escape sequence works. Use the literal characters that represent the bytes you want to remove. If your goal is to normalize line endings and remove them entirely, the code should look like this:

$post_text = $_POST['text'];

// Remove actual carriage return and line feed bytes
$post_text = str_replace("\r\n", '', $post_text);
$post_text = str_replace("\r", '', $post_text);
$post_text = str_replace("\n", '', $post_text);

// Now apply your security filters
$post_text = htmlspecialchars($post_text);
$post_text = mysql_real_escape_string($post_text); // Deprecated in favor of PDO or mysqli prepared statements

Notice the absence of backslashes before r and n inside the double quotes. This tells PHP to insert the actual control characters into the string. If you use single quotes like '\r\n', PHP does not interpret the escape sequence, and you end up with a literal backslash character in your search string, which won't match the binary data in your database entry.

A critical pitfall here is the order of operations regarding security. You are currently using mysql_real_escape_string combined with htmlspecialchars. While htmlspecialchars prevents XSS attacks by escaping special characters for HTML output, mysql_real_escape_string is specifically designed for SQL queries. Using both simultaneously is inefficient and can lead to logic errors if you later switch to prepared statements. Furthermore, the mysql_* extension has been deprecated and removed in modern PHP versions. You should migrate to PDO or MySQLi with prepared statements immediately. Prepared statements handle escaping automatically and are immune to injection attacks, rendering mysql_real_escape_string unnecessary and dangerous to keep.

If you find yourself needing to normalize line endings frequently, consider using the stripslashes function if you suspect backslashes are being double-escaped by your application framework or encoding layer, though this is less common in standard form submissions. The most robust solution remains ensuring your input stream contains the actual bytes you expect and cleaning them with literal string replacements before any security filtering occurs.

For more complex sanitization needs or when migrating legacy codebases to modern standards, resources like https://laravelcompany.com offer guides on updating from deprecated MySQL extensions to safe, modern practices that handle these edge cases without manual byte manipulation.

Our network