如何在textarea替换\r\n ?

时间:2021-04-15 09:37:36

PHP Code

PHP代码

<?php
$content = "check1\r\ncheck2\r\ncheck3\r\nend..."   
$order   = array("\r\n");
$replace = "\n";
$content= str_replace( $order, $replace, $content);
$smarty->assign('content', $content);
?>

View page (a smarty template)

查看页面(smarty模板)

<textarea>{$content}<textarea>

Output:

输出:

check1\r\ncheck2\r\ncheck3\r\nend...

I expect output like below

我希望输出如下。

check1
check2
check3
end...

inside the textarea. I had replaced \r\n into \n, but even then I can't get my desired output. Whats wrong in my code? Thank you in advance.

在文本区域。我已经把\r\n替换成\n,但即使那样,我也无法得到我想要的输出。我的代码有问题吗?提前谢谢你。

4 个解决方案

#1


6  

It looks like you're having a similar problem to this.

看起来你也有类似的问题。

If I'm reading correctly, try:

如果我读的正确,试试:

<textarea>{$content|stripcslashes}<textarea>

#2


3  

I'm no Smarty guru, but you might want this in your template instead:

我不是Smarty guru,但是您可以在模板中使用这个:

<textarea>{$content|stripslashes}<textarea>

#3


1  

This replaces all \r\n with \n.

这将取代所有的\ \n和\n。

$content = str_replace("\r\n", "\n", $content);

I have used that successfully a couple of days ago, and they come out correctly in a text field.

几天前,我成功地使用了它,它们在文本框中正确地显示出来。

#4


0  

I had the same problem with the \r\n, but I had to add extra backslashes to the search argument (i.e., \r\n becomes \\r\\n) to make it work:

我也有同样的问题,但是我必须在搜索参数中添加额外的反斜杠。,\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\

$content = str_replace("\\r\\n","\r\n",$content);

$ = str_replace内容(“\ \ r \ \ n”,“\ r \ n”,美元内容);

#1


6  

It looks like you're having a similar problem to this.

看起来你也有类似的问题。

If I'm reading correctly, try:

如果我读的正确,试试:

<textarea>{$content|stripcslashes}<textarea>

#2


3  

I'm no Smarty guru, but you might want this in your template instead:

我不是Smarty guru,但是您可以在模板中使用这个:

<textarea>{$content|stripslashes}<textarea>

#3


1  

This replaces all \r\n with \n.

这将取代所有的\ \n和\n。

$content = str_replace("\r\n", "\n", $content);

I have used that successfully a couple of days ago, and they come out correctly in a text field.

几天前,我成功地使用了它,它们在文本框中正确地显示出来。

#4


0  

I had the same problem with the \r\n, but I had to add extra backslashes to the search argument (i.e., \r\n becomes \\r\\n) to make it work:

我也有同样的问题,但是我必须在搜索参数中添加额外的反斜杠。,\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\

$content = str_replace("\\r\\n","\r\n",$content);

$ = str_replace内容(“\ \ r \ \ n”,“\ r \ n”,美元内容);