如何回显MySQL数据库中的换行符? [英] How do I echo line breaks from a MySQL database?

查看:116
本文介绍了如何回显MySQL数据库中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个简单的墙贴功能,例如在FaceBook上.

I have created a simple wall post feature, like on FaceBook.

用户写一个帖子,该帖子被提交到数据库,然后回显到网站上;一切正常.

A user writes a post, the post is submitted to the database and then echoed back onto the website; that all works.

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

The only issue is, when the text is echoed back onto the website the line break isn't. So, I typed:

嘿,这是一个帖子.

"Hey this is a post.

这是一个新段落"

,它显示为:

嘿,这是帖子.这是一个新段落"

"Hey this is a post. Here is a new paragraph"

我在这里看到一些帖子说要使用nl2br()函数并在新行中输入/n,但是我真的不希望我的用户每次想要一个新行时都必须写"/n"行,他们只需要按键盘上的Enter键即可.

I've seen a few posts here saying to use the nl2br() function and input /n for a new line, however I really don't want my users to have to write '/n' everytime they want a new line, they should just have to press the enter key on the keyboard.

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

The line break is stored in the database, so I have no idea why it isn't echoed out. Can anyone help?

不确定该代码是否必要,但我会以防万一.

Not sure if the code will be necessary but I'll post it just in case.

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 function nl2br converts newlines to "<br>" breaks.
So, $wallpost=nl2br($wallpost); should accomplish the task.

这篇关于如何回显MySQL数据库中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆