我创建了一个简单的墙贴功能,就像在 Facebook 上一样。
用户撰写帖子,帖子被提交到数据库,然后回显到网站上;这一切都有效。
唯一的问题是,当文本回显到网站时,换行符却没有回显。所以,我输入:
"Hey this is a post.
Here is a new paragraph"
它显示为:
"Hey this is a post. Here is a new paragraph"
我在这里看到一些帖子说使用 nl2br() 函数并输入/n 作为新行,但是我真的不希望我的用户每次想要新行时都必须编写 '/n'行,他们只需按键盘上的 Enter 键即可。
换行符存储在数据库中,所以我不知道为什么它没有回显。有人可以帮忙吗?
不确定代码是否必要,但我会发布它以防万一。
while($wallposts = mysql_fetch_assoc($getwallposts)) {
$postid = $wallposts['id'];
$postedby_username = $wallposts['postedby'];
$wallpostdate = $wallposts['dateposted'];
$wallpost = $wallposts['post'];
$querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");
//get the info above
if (mysql_num_rows($querypostedby_info)===1) {
$getpostedby_info = mysql_fetch_assoc($querypostedby_info);
$postedby_id = $getpostedby_info['id'];
$postedby_profilepicture = $getpostedby_info['profilepicture'];
}
//display the posts
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
}
最佳答案
PHP函数
NL2br
将换行符转换为“<br>
”分隔符。
所以,$wallpost=nl2br($wallpost);
应该完成任务。
关于php - 如何从 MySQL 数据库中回显换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35390453/