CakePHP 清理换行 \n
我有一个评论框,当有人按 Enter 时,Cake 会将 \n (换行)放入数据库中。当我从数据库检索它时,它的显示方式就像 \n 而不是实际的中断或 HTML a
。
似乎没有处理 \n 的选项,我无法理解,因为在文本区域中按 Enter 键很常见。我做错了什么?
谢谢 克里斯
I have a comment box and when somebody presses enter, Cake will put a \n (line feed) into the database. When I retrieve this from the db, it is displayed just like \n instead of an actual break or in HTML a
.
There seems to be no option for handling \n, which I cannot understand because it is common to push enter in a textarea. What am I doing wrong?
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你忘记了有 php 函数涵盖了这一点:)
nl2br() 是您正在寻找的
注:
h() 是出于安全原因。始终首先使用 h(),而不是 nl2br()。
如果你有 html 内容,你就不需要 h() 。 h() 仅适用于纯文本。
提示:您甚至可以增强烘焙模板以自动正确显示 - 请参阅http://www.dereuromark.de/2010/06/22/cake-bake-custom-templates/了解详细信息
you forget that there are php functions covering that :)
nl2br() is what you are looking for
NOTE:
h() is for security reasons. always use h() first, than nl2br().
you dont need h() if you have html content. h() is only for plain text.
TIP: you can even enhance your bake templates to automatically display it correctly - see http://www.dereuromark.de/2010/06/22/cake-bake-custom-templates/ for details
你也可以尝试
echo str_replace("\\n", "
", $variable);
You can also try
echo str_replace("\\n", "<br />", $variable);
CakePHP 有它自己的方法:
检查文档: https: //book.cakephp.org/2.0/en/core-libraries/helpers/text.html
CakePHP has it's own method for that:
Check the documentation: https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html