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']))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?