如何用
替换换行符或 \r\n?

发布于 2024-11-06 14:52:01 字数 312 浏览 4 评论 0原文

我试图简单地替换一些新行,并尝试了三种不同的方法,但我没有得到任何改变:

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

这些应该都有效,但我仍然得到换行符。它们是双的:“\r\r”。这不应该使这些失败,对吧?

I am trying to simply replace some new lines and have tried three different ways, but I don't get any 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

一绘本一梦想 2024-11-13 14:52:01

已经有 nl2br() 函数插入 < code>
标签在新行字符之前:

示例 (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);
?>

但如果是仍然无法正常工作,请确保文本 $desciption 用双引号引起来。

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

注意:与双引号和定界符语法不同,特殊字符的变量和转义序列在单引号字符串中出现时不会被扩展。

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.

心的位置 2024-11-13 14:52:01

尝试使用这个:

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

Try using this:

$description = preg_replace("/\r\n|\r|\n/", '<br/>', $description);
缱绻入梦 2024-11-13 14:52:01

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

如果您非常确定 '\r' 或 '\n' 字符串也应该被替换,我在这里讨论的不是特殊字符,而是两个字符 '\' 和 'r' 的序列,然后转义 '\ ' 在替换字符串中,它将起作用:

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

You may have real characters "\" in the string (the single quote strings, as said @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:

str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br/>",$description);
病毒体 2024-11-13 14:52:01

试试这个:

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

Try this:

echo str_replace(array('\r\n', '\n\r', '\n', '\r'), '<br>', $description);
只为守护你 2024-11-13 14:52:01

nl2br() 正如您所拥有的那样,它应该可以正常工作:

$description = nl2br($description);

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

...$description');

nl2br() as you have it should work fine:

$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');
挽清梦 2024-11-13 14:52:01

nl2br() 对我有用,但我需要用双引号将变量括起来:

这有效:

$description = nl2br("$description");

这不起作用:

$description = nl2br($description);

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

This works:

$description = nl2br("$description");

This doesn't work:

$description = nl2br($description);
二手情话 2024-11-13 14:52:01

这肯定会起作用:

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

This will work for sure:

str_replace("\\r", "<br />", $description); 
str_replace("\\n", "<br />", $description); 
无敌元气妹 2024-11-13 14:52:01
$description = nl2br(stripcslashes($description));
$description = nl2br(stripcslashes($description));
她如夕阳 2024-11-13 14:52:01

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

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

や三分注定 2024-11-13 14:52:01

如果您使用 nl2br,则所有出现的 \n\r 都将替换为
。但是,如果(我不知道是怎么回事)您仍然得到新行,您可以

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

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

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

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

to replace unnecessary new lines by an empty string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文