写入 MySQL 时保留 TextArea 的换行符 [英] Preserve Line Breaks From TextArea When Writing To MySQL

查看:45
本文介绍了写入 MySQL 时保留 TextArea 的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 textarea 使用户能够输入评论.但是,如果用户输入新行,则输出时不会出现新行.有什么办法可以让换行符保持不变.

知道如何保留换行符吗?

解决方案

两个解决方案:

  1. PHP 函数 nl2br():

    例如

    echo nl2br("This\r\nis\n\ra\nstring\r");//会输出这<br/>是<br/><br/>字符串

  2. 将输入包裹在

    标签中.

    请参阅: W3C Wiki - HTML/Elements/pre

I'm using a textarea to enable users to input comments. However, if the users enters new lines, the new lines don't appear when they are outputted. Is there any way to make the line breaks stay.

Any idea how do preserve the line breaks?

解决方案

Two solutions for this:

  1. PHP function nl2br():

    e.g.,

    echo nl2br("This\r\nis\n\ra\nstring\r");
    
    // will output
    This<br />
    is<br />
    a<br />
    string<br />
    

  2. Wrap the input in <pre></pre> tags.

    See: W3C Wiki - HTML/Elements/pre

这篇关于写入 MySQL 时保留 TextArea 的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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