php nl2br 不适合我

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3498930/
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 09:59:55  来源:igfitidea点击:

nl2br not working for me

php

提问by sebb

I can't get nl2brfunction to work after fetching data from my database:

nl2br从我的数据库中获取数据后,我无法让函数工作:

$result = mysql_query("SELECT comments..etc.etc..");

while ($row = mysql_fetch_array($result))
{
  echo nl2br($row["comments"]);
}

In database row comments:

在数据库行中comments

\r\nThanks,\r\n

OUTPUT:

输出:

Same as in DB:

与数据库相同:

\r\nThanks,\r\n

If I simply test this out like so it works fine:

如果我简单地测试一下,它可以正常工作:

<?php
$mystring = "\r\nThanks,\r\n";
echo nl2br($mystring);
?>

OUTPUT:

输出:

converts \r \n to <br />

采纳答案by Your Common Sense

Most likely you are doing escaping twice, when adding your data into DB.
Check your code that adds data to DB and remove unnecessary escaping.

在将数据添加到数据库时,您很可能进行了两次转义。
检查将数据添加到数据库的代码并删除不必要的转义。

Most likely it's some senseless "universal sanitization" function.

很可能是某种毫无意义的“通用消毒”功能。

Well it's easy.
Let's take a quote, not a newline to demonstrate. The behavior the same. Slashes being stripped then data goes to database.

嗯,这很容易。
让我们引用,而不是换行来演示。行为相同。斜线被剥离,然后数据进入数据库。

Thus, in the normal case:

因此,在正常情况下:

source: It's
after escaping: It\'s
by the query execution slash being stripped and
both in the database and back It's

来源:It's
转义后:It\'s
通过查询执行斜杠被剥离并且
在数据库中和返回It's

in double escaping case:

在双重转义情况下:

source: It's
after escaping: It\'s
after second escaping: It\\\'s
by the query execution slash being stripped and
both in the database and back It\'s
we have our data spoiled.

来源:It's
转义后:It\'s
第二次转义后:It\\\'s
通过查询执行斜杠被剥离,并且
在数据库和后面,It\'s
我们的数据都被破坏了。

Just make yourself understand that escaping i not something magical that makes your data "safe" (and, therefore can be done many times, as you probably think). It's just adding a backslash to certain symbols.

只要让自己明白,转义 i 并不是让您的数据“安全”的魔法(因此可以多次完成,正如您可能认为的那样)。它只是为某些符号添加反斜杠。

回答by bcosca

try this:

尝试这个:

echo preg_replace('/\v+|\\r\\n/Ui','<br/>',$row["comments"]);

回答by DangerPaws

I know this is an old post but if like me you are have stumbled across this issue and the above didn't work for you, this solution may help you instead:

我知道这是一篇旧帖子,但如果你像我一样偶然发现了这个问题并且上述方法对你不起作用,这个解决方案可能会对你有所帮助:

echo nl2br(stripslashes($row["comments"]));

or(they are not the same function, note the additional "c" after strip)

(它们不是相同的功能,请注意剥离后的附加“c”)

echo nl2br(stripcslashes($row["comments"]));

See original thread that helped me: nl2br() not working when displaying SQL results

查看对我有帮助的原始线程:nl2br() 在显示 SQL 结果时不起作用

回答by Christian Mann

My guess is that the slashes in your DB are literal slashes (followed by n or r), not newlines. Can you find a way to store literal newlines in your database?

我的猜测是您的数据库中的斜杠是字面斜杠(后跟 n 或 r),而不是换行符。你能找到一种方法在你的数据库中存储文字换行符吗?

回答by bhushya

Following solution will work for both windows as well as for linux/unix machine

以下解决方案适用于 Windows 以及 linux/unix 机器

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

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

回答by Gzom

Make sure that you are not to using strings from file and/or set in single apostrophe. Because string is treated literally, and nl2br will not work. NL2BR will work with double apostrophe.

确保您不使用来自文件和/或设置在单个撇号中的字符串。因为字符串是按字面处理的,nl2br 不起作用。NL2BR 将使用双撇号。

回答by blockhead

Building on what Christian is saying, why don't you trying replacing the literal '\r\n'with "\r\n"?

建立在什么基督徒说,你为什么不尝试替换文字'\r\n'"\r\n"

回答by Dinesh Patil

Data you have stored is allready added the slashes.

您存储的数据已经添加了斜线。

You have to use stripslashes() first then str_replace()

您必须先使用 stripslashes() 然后使用 str_replace()

stripslashes(str_replace('\r\n','<br/>',$row["comments"]))

回答by JamieTom

For some reason this didn't work for me...

出于某种原因,这对我不起作用......

echo nl2br(stripcslashes($row["comments"]));

But this did...

但这确实...

$comments = stripcslashes($row["comments"]);

$commentsWithBreaks = nl2br($comments);

echo $commentsWithBreaks;

回答by jdisla

Not working for me either. I just did the following:

也不为我工作。我只是做了以下事情:

$mensaje = str_replace("&#10;", "<br/>", $mensaje);

$mensaje = str_replace(" &#10;", " <br/>", $mensaje);