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

标签 php mysql echo

我创建了一个简单的墙贴功能,就像在 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'>&bull; $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/

相关文章:

PHP 读取文件错误

javascript - Laravel:更改文本框编辑上的元素

php - 大型数据库的备份

mysql - 使用 != 而不是 < 时得到错误结果

php - 为什么我的 PHP 函数没有产生任何输出?

php - 在 PHP 中回显 HTML

php - 将android应用程序链接到mysql数据库手机并链接信息

php - 使用 PHP 和 MySQL 通过比较逗号分隔的字符串列值从表中获取值

php - 有没有办法在 PHP 中重定向输出?

php - 使用 PHP 解析 unicode 表情符号文本文件