如何从 MySQL 数据库中回显换行符?

2023-12-07

我创建了一个简单的墙贴功能,就像在 Facebook 上一样。

用户撰写帖子,帖子被提交到数据库,然后回显到网站上;这一切都有效。

唯一的问题是,当文本回显到网站上时,换行符却没有回显。所以,我输入:

“嘿,这是一个帖子。

这是一个新段落”

它显示为:

“嘿,这是一篇文章。这是一个新段落”

我在这里看到一些帖子说使用 nl2br() 函数并输入 /n 作为新行,但是我真的不希望我的用户每次想要新行时都必须编写“/n”,他们应该只需要按键盘上的回车键即可。

换行符存储在数据库中,所以我不知道为什么它没有回显。有人可以帮忙吗?

不确定代码是否必要,但我会发布它以防万一。

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'>&bull; $wallpostdate</div> 
        </div>
        <div id='wallpost-content'>
            $wallpost
        </div>
    </div>
    ";
}

PHP函数 NL2br 将换行符转换为“<br>” 休息。
So, $wallpost=nl2br($wallpost);应该完成任务。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 MySQL 数据库中回显换行符? 的相关文章

随机推荐