摆脱 \r\n 字符串
我有一个表单,在其中输入了一个换行符,当我输入它时,该换行符看起来是正确的,但是当现在从数据库中提取数据时,我显示的是 \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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然。
您的代码进行了不必要的转义,很可能是在将文本添加到数据库之前。
因此,您必须找到有害代码并将其删除,而不是将其替换回来。
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.
这意味着,数据库中可能有纯文本“\n\r”字符串。
尝试在显示之前清理数据库输出:(
只是猜测)。
附录:
当然,正如Col. Shrapnel指出,有一些根本性的错误
与数据库的内容(或者,按照惯例以这种方式使用,而您不知道)。
目前,您已部分修复了症状
但最好找出这些转义字符的原因
根本就在数据库中。
问候
rbo
This means, you have probably plain text '\n\r' strings in the db.
Try to sanitize db output before display:
(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
您可以使用 str_replace 来清理输入。
You can use str_replace to clean up the input.
您可能还想了解 PHP 中单引号和双引号之间的区别: http://php.net/manual/en/language.types.string.php
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