字符串替换 <br /> 到 \n 给出双中断
string replace <br /> to \n gives double breaks
解决方案信息:
nl2br 函数没有像我预期的那样将 \n
替换为 <br />
,而是在 \n
.
之前插入了一个 <br />
来自 php.net:
nl2br — Inserts HTML line breaks before all newlines in a string
原题:
我将 PHP 中的 <br />
元素替换为 \n
,这是我的输入:
Top spot!<br />
<br />
123456789 123456789 123456789
这是我的代码:
$commenttext = str_replace("<br />", "\n", $reviewdata['comment']);
但我的输出是:
Top spot!
123456789 123456789 123456789
我在使用 str_replace 时是否遗漏了什么?使用它后,我获得了双倍的休息时间。
让我告诉你。您替换前的代码:
Top spot!<br />\n<br />\n123456789 123456789 123456789
替换后的代码:
Top spot!\n\n\n\n123456789 123456789 123456789
如您所见,<br />
已正确替换为新行。
首先尝试用新行替换 <br />
标签:
$commenttext = str_replace("<br />\n", "\n", $reviewdata['comment']);
<?php
$text = 'top spot!<br />
<br />
123456789 123456789 123456789';
echo str_replace("<br />\n","\n",$text);
?>
OUTPUT
top spot! 123456789 123456789 123456789
解决方案信息:
nl2br 函数没有像我预期的那样将 \n
替换为 <br />
,而是在 \n
.
<br />
来自 php.net:
nl2br — Inserts HTML line breaks before all newlines in a string
原题:
我将 PHP 中的 <br />
元素替换为 \n
,这是我的输入:
Top spot!<br />
<br />
123456789 123456789 123456789
这是我的代码:
$commenttext = str_replace("<br />", "\n", $reviewdata['comment']);
但我的输出是:
Top spot!
123456789 123456789 123456789
我在使用 str_replace 时是否遗漏了什么?使用它后,我获得了双倍的休息时间。
让我告诉你。您替换前的代码:
Top spot!<br />\n<br />\n123456789 123456789 123456789
替换后的代码:
Top spot!\n\n\n\n123456789 123456789 123456789
如您所见,<br />
已正确替换为新行。
首先尝试用新行替换 <br />
标签:
$commenttext = str_replace("<br />\n", "\n", $reviewdata['comment']);
<?php
$text = 'top spot!<br />
<br />
123456789 123456789 123456789';
echo str_replace("<br />\n","\n",$text);
?>
OUTPUT
top spot! 123456789 123456789 123456789