php 如何用 <br/> 替换换行符或 \r\n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5946114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:18  来源:igfitidea点击:

How to replace newline or \r\n with <br/>?

phphtmlline-breaksnl2br

提问by Thomas McCabe

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

试图简单地替换一些新行。尝试了 3 种不同的方法,但没有任何变化:

$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\r”。这不应该使任何这些失败,对吗?

回答by Robik

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

已经有一个nl2br()功能可以在换行符之前替换插入<br>标签:

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 $desciptionis double-quoted.

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

That's because single quotes do not 'expand' escape sequences such as \ncomparing 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 语法不同,特殊字符的变量和转义序列在出现在单引号字符串中时不会被扩展。

回答by afarazit

Try using this

尝试使用这个

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

回答by regilero

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);

回答by sakatc

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...

'示例代码第一行的未关闭更有可能导致您的问题。删除 $description 之后的 '...

...$description');

回答by infografnet

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);

回答by l2aelba

Try this:

尝试这个:

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

回答by Radeck

This will work for sure:

这肯定会起作用:

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

回答by softcod.com

$description = nl2br(stripcslashes($description));

回答by Dipak Chatterjee

I think str_replace(array("\r\n", "\r", "\n"), " ", $string); Will work.

我认为 str_replace(array("\r\n", "\r", "\n"), " ", $string); 将工作。

回答by Evgeniy Skulditsky

If you are using nl2brall occurrences of \nand \rwill be replaced by <br>. But if (i dont know how it is) you still get new lines you can use

如果您使用nl2br所有出现的\n\r将被替换为<br>。但是如果(我不知道它是怎么回事)你仍然可以使用新的线路

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

To replase unnecessary new lines by empty string

用空字符串替换不必要的新行