Replacing \R\N with PHP

Replacing \r\n with PHP

The main problem you have with all the variations you've tried is that both \n and \r are escape characters that are only escaped when you use them in a double-quoted string.

In PHP, there is a big difference between '\r\n' and "\r\n". Note the single-quotes in the first, and double-quotes in the second.

So: '\r\n' will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "\r\n" will contain two characters, those being the new line and carriage return characters.

So the direct answer to your question is that you need to use the second of the examples you gave in the question, but with double quotes instead of single quotes:

$text = str_replace("\r\n",'', $text);

It's worth pointing out that this will remove all new lines from the input, and replace them with nothing. If there is more than one new line in the input, they will all be removed. Without knowing more about your application, I don't know if this is what you want, but here are some thoughts:

  • If you only want to remove blank lines (and other white space) from the end of the input string, you can use the trim() function instead.

  • If you want to retain the formatting for output to HTML, then the nl2br() function will help you. If you want to output to HTML without the formatting, then you may not need to remove them, as the browser will not render line breaks from \n characters.

  • If you replace new lines with nothing, as per your example, the last word of the first line will now run directly into the first word of the second line, and so on. You may prefer to replace them with a space character rather than nothing.

  • It is possible that users may submit the input with only \n without the matching \r, or vice-versa (this may be due to their OS or browser, or a deliberate hack, or a number of other reasons). If you want to replace all instances of both these characters, a more reliable way to do it would be to replace them individually, rather than relying on them being next to one-another. str_replace() allows you to do this by specifying them in an array. You can also use strtr() or preg_replace() to achieve the same goal.

PHP: Replace \r\n with a real line break

// Note: $source is assigned with single quotes, which means that
// the \r\n will not be interpolated
$source = 'Nice sentence.\r\nAlso a nice sentence.';
$destination = str_replace(array('\r', '\n'), array("\r", "\n"), $source);

How can I replace newline or \r\n with br/?

There is already the nl2br() function that inserts <br> tags before new line characters:

Example (codepad):

<?php
// Won't work
$desc = 'Line one\nline two';
// Should work
$desc2 = "Line one\nline two";

echo nl2br($desc);
echo '<br/>';
echo nl2br($desc2);
?>

But if it is still not working make sure the text $desciption is double-quoted.

That's because single quotes do not 'expand' escape sequences such as \n comparing to double quoted strings. Quote from PHP documentation:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

How to REPLACE \n with br in PHP

If you are echo'ing the data out and you see an actual \n in the output, then what you have isn't not a newline character, but a literal \ followed by n character. Usually these are translated to a newline if in code, but if in string in a variable (such as when pulled from a database), it isn't. You should then do a str_replace on '\n' (notice the single quotes so it isn't translated to a newline character).

This is likely either done by you on insert using something like addslashes (would covert a \n newline to \\n) or possibly done automatically by the database for some arbitrary security reason and not translated back.

str_replace(\r\n) replaces twice, while str_replace(\n) replaces once

There's two newlines, so replace them both

$movie_description = str_replace("\r\n\r\n","[br]",$_description);

Problem Replacing Literal String \r\n With Line Break in PHP

UTF-16 is the problem. If you're just working with raw the bytes, then you can use the full sequences for replacing:

$out = str_replace("\x00\x5c\x00\x72\x00\x5c\x00\x6e", "\x00\x0a", $in);

This assumes big-endian UTF-16, else swap the zero bytes to come after the non zeros:

$out = str_replace("\x5c\x00\x72\x00\x5c\x00\x6e\x00", "\x0a\x00", $in);

If that doesn't work, please post a byte-dump of your input file so we can see what it actually contains.

Cannot replace carriage return and new line in php

Problem :
Your str_replace is not working because you are using double quotes.

Solution :
You should replace your double quotes with single quotes and then the magic will happen :D

$string = str_replace(['\r\n','\r','\n'], '\\n', $string);

EXTRA USEFUL INFORMATION : For more you should take a look at for details as it's useful to get to know the difference between double quotes and single quotes as:

What is the difference between single-quoted and double-quoted strings in PHP?

str_replace not replacing br with \r\n but \\r\\n

Use double quotes (") rather than single quotes (') for regex stuff (like \r\n):

str_replace("<br>", "\r\n", $string);

How to replace \r\n with a line break

You don't use htmlentities on the way in, you use it on the output. You should be escaping to input into your database and then to display newlines on the output consider:

nl2br(htmlentities($comment));

How to replace all new lines of string with `BR` in php?

Microsoft Windows / MS-DOS: \r\n

Acorn BBC and RISC OS spooled text output: \n\r

Apple Macintosh OS 9 and earlier: \r

Unix (e.g., Linux), also Apple OS X and higher: \n

replace -> nl2br

more on that here

If new lines exists in the textarea than \n,\r or \r\n are present! These are control characters which are not outputted.

To remove them you could try:

$string = str_replace(array("\r\n", "\n\r", "\r", "\n"), "", $string);


Related Topics



Leave a reply



Submit