如何用替换\r & \n ?

时间:2022-07-20 09:37:04

Trying to simply replace some new lines. Have tried 3 different ways and I get no change:

尝试简单地替换一些新的行。尝试了三种不同的方法,我没有改变:

$description = preg_replace('/\r?\n|\r/','<br/>', $description);
$description = str_replace(array("\r\n","\r","\n"),"<br/>", $description);
$description = nl2br($description);

These should all work but I still get the newlines. They are double: "\r\r". That shouldn't make any of these fail right?

这些都可以,但我还是得到了换行。他们是双:“\ r \”。这不应该让其中任何一个失败,对吗?

8 个解决方案

#1


96  

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

已经有nl2br()函数在新行字符之前替换插入
标记:

Example (codepad):

例子(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.

但是如果它仍然不工作,请确保文本$desciption是双引号。

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

这是因为单引号不会“扩展”转义序列,比如\n,与双引号字符串比较。从PHP文档引用:

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.

注意:不像双引号和heredoc语法,特殊字符的变量和转义序列不会在单个引用字符串中出现。

#2


51  

Try using this

试着用这

$description = preg_replace("/\r\n|\r|\n/",'<br/>',$description);

#3


13  

You may have real characters "\" in the string (the single quote strings, as said @Robik).

您可能在字符串中有真正的字符“\”(单引号字符串,如@Robik)。

If you are quite sure the '\r' or '\n' strings should be replaced as well, I'm not talking of special characters here but a sequence of two chars '\' and 'r', then escape the '\' in the replace string and it will work:

如果你很确定“\r”或“\n”字符串也应该被替换,我这里不是说特殊字符,而是两个字符的“\”和“r”的序列,然后在替换字符串中脱离“\”,它会工作:

str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br/>",$description);

#4


5  

nl2br() as you have it should work fine:

nl2br()如你所拥有,它应该可以正常工作:

$description = nl2br($description);

It's more likely that the unclosed ' on the first line of your example code is causing your issue. Remove the ' after $description...

更有可能的是,在您的示例代码的第一行上的“未关闭”导致了您的问题。删除“after $description…”

...$description');

#5


2  

nl2br() worked for me, but I needed to wrap variable with double quotes:

nl2br()为我工作,但我需要用双引号来包装变量:

This works:

如此:

$description = nl2br("$description");

This doesn't work:

这并不工作:

$description = nl2br($description);

#6


0  

This will work for sure:

这肯定会奏效:

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

#7


0  

Try this:

试试这个:

echo str_replace( array('\r\n','\n\r','\n','\r'), '<br>' , $description );

#8


-2  

If you are using nl2br all occurrences of \n and \r will be replaced by <br>. But if (i dont know how it is) you still get new lines you can use

如果你正在使用nl2br,所有发生的\n和\r将被
取代。但是如果(我不知道是怎么回事),你仍然可以使用新的线路。

str_replace("\r","",$description);
str_replace("\n","",$description);

To replase unnecessary new lines by empty string

用空字符串回复不必要的新行。

#1


96  

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

已经有nl2br()函数在新行字符之前替换插入
标记:

Example (codepad):

例子(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.

但是如果它仍然不工作,请确保文本$desciption是双引号。

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

这是因为单引号不会“扩展”转义序列,比如\n,与双引号字符串比较。从PHP文档引用:

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.

注意:不像双引号和heredoc语法,特殊字符的变量和转义序列不会在单个引用字符串中出现。

#2


51  

Try using this

试着用这

$description = preg_replace("/\r\n|\r|\n/",'<br/>',$description);

#3


13  

You may have real characters "\" in the string (the single quote strings, as said @Robik).

您可能在字符串中有真正的字符“\”(单引号字符串,如@Robik)。

If you are quite sure the '\r' or '\n' strings should be replaced as well, I'm not talking of special characters here but a sequence of two chars '\' and 'r', then escape the '\' in the replace string and it will work:

如果你很确定“\r”或“\n”字符串也应该被替换,我这里不是说特殊字符,而是两个字符的“\”和“r”的序列,然后在替换字符串中脱离“\”,它会工作:

str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br/>",$description);

#4


5  

nl2br() as you have it should work fine:

nl2br()如你所拥有,它应该可以正常工作:

$description = nl2br($description);

It's more likely that the unclosed ' on the first line of your example code is causing your issue. Remove the ' after $description...

更有可能的是,在您的示例代码的第一行上的“未关闭”导致了您的问题。删除“after $description…”

...$description');

#5


2  

nl2br() worked for me, but I needed to wrap variable with double quotes:

nl2br()为我工作,但我需要用双引号来包装变量:

This works:

如此:

$description = nl2br("$description");

This doesn't work:

这并不工作:

$description = nl2br($description);

#6


0  

This will work for sure:

这肯定会奏效:

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

#7


0  

Try this:

试试这个:

echo str_replace( array('\r\n','\n\r','\n','\r'), '<br>' , $description );

#8


-2  

If you are using nl2br all occurrences of \n and \r will be replaced by <br>. But if (i dont know how it is) you still get new lines you can use

如果你正在使用nl2br,所有发生的\n和\r将被
取代。但是如果(我不知道是怎么回事),你仍然可以使用新的线路。

str_replace("\r","",$description);
str_replace("\n","",$description);

To replase unnecessary new lines by empty string

用空字符串回复不必要的新行。