douju8113 2015-05-23 17:54
浏览 69
已采纳

htmlentities()会删除返回字符和回车吗?

I am creating a forum with PHP and MySQL with the help of PDO class. I have just started coding the forum and come up with a problem.

What I am doing is that

$post_body = htmlentities($_POST['post_body']);

get the user input like this and then send this to my function that then query this into the database like this

$str = $this->database->prepare('INSERT INTO `blah`,`blah_blah` VALUES(?,?)');
$str->bindValue(1,$someVal);
$str->bindvalue(2,$post_body);

$str->execute();

and when I display these details i do something like this

html_entity_decode($postDetails['post_body']); //$postDetails has been initialized correctly

Yes I have added try catches and handled the exceptions; but since this is a forum post, when a user presses return key, he/she expects to have a new line in the posted article. but when i display these posts i am losing every return/new lines in the post. Why and how do I get around this.

Explanation about wht this happens would be much appreciated!

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsapkqaduj6718493 2015-05-23 18:01
    关注

    is not an html entity. It will not be decoded by that function.

    Use nl2br to accomplish this like so:

    echo nl2br(html_entity_decode($postDetails['post_body']))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化