开发者

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

I have a text file that has the literal string \r\n in it. I want to replace this with an actual line break (\n).

I know that the regex /\\r\\n/ should match it (I have tested it in Reggy), but I cannot get it to work in PHP.

I have tried the following variations:

preg_replace("/\\\\r\\\\n/", "\n", $line);

preg_replace("/\\\\[r]\\\\[n]/", "\n", $line);

preg_replace("/[\\\\][r][\\\\][n]/", "\n", $line);

preg_replace("/[\\\\]r[\\\\]n/", "\n", $line);

If I just try to replace the backslash, it works properly. As soon as I add an r, it finds no matches.

The file I am reading is encoded as UTF-16.

Edit:

I have also already tried using str_replace().

I now believe that the problem h开发者_如何学Goere is the character encoding of the file. I tried the following, and it did work:

$testString = "\\r\\n";
echo preg_replace("/\\\\r\\\\n/", "\n", $testString);

but it does not work on lines I am reading in from my file.


Save yourself the effort of figuring out the regex and try str_replace() instead:

str_replace('\r\n', "\n", $string);


Save yourself the effort of figuring out the regex and the escaping within double quotes:

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

For what it is worth, preg_replace("/\\\\r\\\\n/", "\n", $line); should be fine. As a demonstration:

var_dump(preg_replace("/\\\\r\\\\n/", "NL", 'Cake is yummy\r\n\r\n'));

Gives: string(17) "Cake is yummyNLNL"

Also fine is: '/\\\r\\\n/' and '/\\\\r\\\\n/'

Important - if the above doesn't work, are you even sure literal \r\n is what you're trying to match?..


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.


$result = preg_replace('/\\\\r\\\\n/', '\n', $subject);

The regex above replaces the type of line break normally used on windows (\r\n) with linux line breaks (\n).

References:

  • Difference between CR LF, LF and CR line break types?
  • Right way to escape backslash [ \ ] in PHP regex?
  • Regex Explanation


I always keep searching for this topic, and I always come back to a personal line I wrote.

It looks neat and its based on RegEx:

 "/[\n\r]/"

PHP

 preg_replace("/[\n\r]/",'\n', $string )

or

 preg_replace("/[\n\r]/",$replaceStr, $string )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜