doulou0882 2016-02-14 09:53
浏览 133
已采纳

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

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.

Here is a new paragraph"

and it displayed as:

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

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>
    ";
}
  • 写回答

2条回答 默认 最新

  • dsbpaqt61965 2016-02-14 09:56
    关注

    PHP function nl2br converts newlines to "<br>" breaks.
    So, $wallpost=nl2br($wallpost); should accomplish the task.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵