摆脱 \r\n 字符串

发布于 2024-10-20 04:00:37 字数 401 浏览 1 评论 0原文

我有一个表单,在其中输入了一个换行符,当我输入它时,该换行符看起来是正确的,但是当现在从数据库中提取数据时,我显示的是 \n\r 字符串,而不是空格。

我尝试这样做:

$hike_description = nl2br($hike_description);

但它不起作用。有谁知道如何解决这个问题?我正在使用 PHP。 这是发生这种情况的页面。请参阅页面的描述部分: http://www.comehike.com/hikes/scheduled_hike.php?hike_id=130谢谢

, 亚历克斯

I have a form into which I entered a newline character which looked correct when I entered it, but when the data is now pulled from the database, instead of the white space, I get the \n\r string showing up.

I try to do this:

$hike_description = nl2br($hike_description);

But it doesn't work. Does anyone know how this can be fixed? I am using PHP.
And here is the page where this is happening. See the description section of the page:
http://www.comehike.com/hikes/scheduled_hike.php?hike_id=130

Thanks,
Alex

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

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

发布评论

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

评论(4

£冰雨忧蓝° 2024-10-27 04:00:37

有谁知道如何解决这个问题吗?

当然。
您的代码进行了不必要的转义,很可能是在将文本添加到数据库之前。
因此,您必须找到有害代码并将其删除,而不是将其替换回来。

Does anyone know how this can be fixed?

Sure.
Your code doing unnecessary escaping, most likely before adding text to the database.
So, instead of replacing it back, you have to find that harmful code and get rid of it.

游魂 2024-10-27 04:00:37

这意味着,数据库中可能有纯文本“\n\r”字符串。

尝试在显示之前清理数据库输出:(

$sanitized_text = preg_replace('/\\[rn]/','', $text_from_db);

只是猜测)。

附录

当然,正如Col. Shrapnel指出,有一些根本性的错误
与数据库的内容(或者,按照惯例以这种方式使用,而您不知道)。

目前,您已部分修复了症状
但最好找出这些转义字符的原因
根本就在数据库中。

问候

rbo

This means, you have probably plain text '\n\r' strings in the db.

Try to sanitize db output before display:

$sanitized_text = preg_replace('/\\[rn]/','', $text_from_db);

(just a guess).

Addendum:

Of course, as Col. Shrapnel pointed out, there's something fundamentally wrong
with the contents of the database (or, it is used this way by convention and you don't know that).

For now, you have fixed a symptom partially
but it would be much better to look for the reason for these escaped characters
being in the database at all.

Regards

rbo

在巴黎塔顶看东京樱花 2024-10-27 04:00:37

您可以使用 str_replace 来清理输入。

$hike_description = nl2br(str_replace("\r\n", "\n", $hike_description));

You can use str_replace to clean up the input.

$hike_description = nl2br(str_replace("\r\n", "\n", $hike_description));
过度放纵 2024-10-27 04:00:37
$hike_description = str_replace(array('\n','\r'),'',$hike_description);

您可能还想了解 PHP 中单引号和双引号之间的区别: http://php.net/manual/en/language.types.string.php

$hike_description = str_replace(array('\n','\r'),'',$hike_description);

You may want to read up on the differences between the single quote and double quote in PHP as well: http://php.net/manual/en/language.types.string.php

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