简体   繁体   English

\n\r 未被 PHP 函数删除

[英]\n\r Not removed by PHP functions

On my home page, I display some info about the hikes (it is a hiking site) and embarassinlgy, the \r\n characters still show up.在我的主页上,我显示了一些关于远足的信息(它是一个远足网站)并且令人尴尬,\r\n 字符仍然出现。

I do these functons我做这些功能

$hike_description = htmlspecialchars ($hike_description);
$hike_description = nl2br($hike_description);

And still those characters do not go away.而且这些字符仍然没有 go 消失。 You can take a look yourself at http://www.comehike.com你可以自己看看http://www.comehike.com

Would you know what is the proper way to get rid of the \r\n characters?您知道摆脱 \r\n 字符的正确方法是什么吗? You can see it happening in the "Upcoming Hikes" section...on the 3rd hike.你可以在“即将到来的远足”部分看到它发生在第三次远足中。

Thanks!谢谢!

In your case, the following will work:在您的情况下,以下将起作用:

$hike_description = str_replace ( "\\r\\n","<br />", $hike_description);

The text \r\n is literal, rather than control characters.文本\r\n是文字,而不是控制字符。

Try manually replacing the sequence尝试手动替换序列

str_replace( "\r\n", "<br />", $hike_description );

The \n\r in your page are not escape sequences... They are actually a \ character followed by a n character followed by a \ character followed by a r character.您页面中的\n\r不是转义序列...它们实际上是一个\字符,后跟一个n字符,后跟一个\字符,然后是一个r字符。

In your database, you should store that as the actual characters, not the escape sequences.在您的数据库中,您应该将其存储为实际字符,而不是转义序列。 Then, calling nl2br() will work as expected.然后,调用nl2br()将按预期工作。

Yes, you can do a str_replace() , however, you should instead fix the encoding of your data in your database.是的,您可以执行str_replace() ,但是,您应该改为修复数据库中数据的编码。 It will save you trouble in the future.它将为您节省将来的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM