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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号