htmlentities() 是否删除 return 字符和回车符 return?
htmlentities() does it remove return character and carriage return?
我正在 PDO class 的帮助下用 PHP 和 MySQL 创建一个论坛。我刚开始编写论坛代码并遇到了一个问题。
我做的是
$post_body = htmlentities($_POST['post_body']);
像这样获取用户输入,然后将其发送到我的函数,然后像这样将其查询到数据库中
$str = $this->database->prepare('INSERT INTO `blah`,`blah_blah` VALUES(?,?)');
$str->bindValue(1,$someVal);
$str->bindvalue(2,$post_body);
$str->execute();
当我显示这些细节时,我会做这样的事情
html_entity_decode($postDetails['post_body']); //$postDetails has been initialized correctly
是的,我添加了 try catch 并处理了异常;但由于这是一个论坛 post,当用户按下 return 键时,he/she 期望在 posted 文章中有一个新行。但是当我显示这些 post 时,我丢失了 post 中的每 return/new 行。为什么以及如何解决这个问题。
非常感谢解释发生这种情况的原因!
提前致谢。
\n
不是 html 实体。它不会被该函数解码。
使用 nl2br
像这样完成:
echo nl2br(html_entity_decode($postDetails['post_body']))
我正在 PDO class 的帮助下用 PHP 和 MySQL 创建一个论坛。我刚开始编写论坛代码并遇到了一个问题。
我做的是
$post_body = htmlentities($_POST['post_body']);
像这样获取用户输入,然后将其发送到我的函数,然后像这样将其查询到数据库中
$str = $this->database->prepare('INSERT INTO `blah`,`blah_blah` VALUES(?,?)');
$str->bindValue(1,$someVal);
$str->bindvalue(2,$post_body);
$str->execute();
当我显示这些细节时,我会做这样的事情
html_entity_decode($postDetails['post_body']); //$postDetails has been initialized correctly
是的,我添加了 try catch 并处理了异常;但由于这是一个论坛 post,当用户按下 return 键时,he/she 期望在 posted 文章中有一个新行。但是当我显示这些 post 时,我丢失了 post 中的每 return/new 行。为什么以及如何解决这个问题。
非常感谢解释发生这种情况的原因!
提前致谢。
\n
不是 html 实体。它不会被该函数解码。
使用 nl2br
像这样完成:
echo nl2br(html_entity_decode($postDetails['post_body']))