文本区域、换行符和 nl2br 的问题

发布于 2024-12-20 13:40:13 字数 801 浏览 3 评论 0 原文

我无法从数据库中获取数据并在 HTML 页面文本区域中回显。

这是用于将数据存入数据库的代码:

$_SESSION['content'] = mysqli_real_escape_string($link, strip_tags($_POST['content'],'<a>'));

这只是简单地去除除数据库中的链接和存储之外的 HTML 标记。如果您查看数据库,换行符是不可见的,但确实存在,所以我假设它们是 \n 和 \r。

如果我要在文本区域中输入:

This should be a 

New line

数据库将其存储为:

This should be a<br>
New line

当回显到文本区域时,这就是显示的内容:

This should be a \r\n\r\nNew line

我确信我错过了一些非常简单的东西,非常感谢任何帮助。

更新:

如果我删除 mysqli_real_escape_string,换行符将被保留并完美工作,我是否必须为此牺牲安全性?

已解决:

mysqli_real_escape_string 导致问题,不要回显已应用此问题的变量。仅在从数据库插入、删除等时使用 mysqli_real_escape_string,而不是之前,绝对不是之后;)

谢谢大家!

I'm having trouble getting data out of the database and echoing out in a HTML page textarea.

This is the code used to get the data into the database:

$_SESSION['content'] = mysqli_real_escape_string($link, strip_tags($_POST['content'],'<a>'));

This simply strips HTML tags except for links and stores in the database. If you look in the database, the line breaks are invisible but there, so i assume they are \n and \r.

If i were to type into a textarea:

This should be a 

New line

The database stores this as:

This should be a<br>
New line

When echoed out into a textarea, this is what's displayed:

This should be a \r\n\r\nNew line

I'm sure i'm missing something very simple, any help greatly appreciated.

UPDATE:

If i remove mysqli_real_escape_string, the line breaks are preserved and work perfectly, do I have to sacrifice security for this?

SOLVED:

mysqli_real_escape_string causing the problem, do not echo out a variable which has had this applied. Only use mysqli_real_escape_string when inserting, deleting, etc from a database, not before, definitely not after ;)

Thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

极致的悲 2024-12-27 13:40:13

使用正确的 HTML/CSS。

;-)

换行符在 HTML pre 标记中或在 CSS 空白属性设置为:

white-space: pre;

资源的标记中中断所有工作:

http://www.w3schools.com/cssref/pr_text_white-space.asp

http://www.quirksmode.org/css/whitespace.html

Use the correct HTML/CSS.

;-)

The line breaks all work in an HTML pre tag, or in a tag with the CSS white-space property set to:

white-space: pre;

Resources:

http://www.w3schools.com/cssref/pr_text_white-space.asp

http://www.quirksmode.org/css/whitespace.html

深海蓝天 2024-12-27 13:40:13

首先,您不应该使用 nl2br,因为这会将您的 \n 更改为

您很可能需要去掉斜杠echo stripslashes($_SESSION['content'])

编辑以下进一步注释:

如果数据在数据库中存储为
,则只需执行str_replace('
' ,"\n",$string);
会将
转换为 \n

Firstly, you shouldn't be using nl2br as this will change your \n's to <br>'s.

You will most likely need to strip the slashes echo stripslashes($_SESSION['content']).

Edit following further comments:

If the data is stored as <br>'s in the database, you can just do str_replace('<br>',"\n",$string); which will convert the <br>'s into \n's

两相知 2024-12-27 13:40:13

这对我有用:

function br2nl( $input ) {
    return preg_replace( '/\<br.*\>/Ui', '', $input );
}

This worked for me:

function br2nl( $input ) {
    return preg_replace( '/\<br.*\>/Ui', '', $input );
}
小草泠泠 2024-12-27 13:40:13

对于很长的行,您还需要文本换行,而不是可能脱离其父容器。要覆盖这种情况,同时保留新行,请使用以下 css:

white-space: pre-wrap;

资源: https:// /css-tricks.com/almanac/properties/w/whitespace/

For very long lines, you also need the text to wrap instead of potentially break out of its parent container. To cover this case, but also preserve new lines, use following css:

white-space: pre-wrap;

resource: https://css-tricks.com/almanac/properties/w/whitespace/

木森分化 2024-12-27 13:40:13

nl2br 函数将该内容作为字符串处理:
http://codepad.org/M2Jw9KQJ

这让我相信您并没有重复您声称的内容是。

你确定数据库里的内容是你说的那样吗?
也许您需要反过来:

function br2nl( $data ) {return preg_replace( "!!iU", "\n", $data );}

The nl2br function is working with that content as a string:
http://codepad.org/M2Jw9KQJ

This leads me to believe that you are not echoing out the content you claim you are.

Are you sure the content in the DB is as you say it is?
Perhaps you need to go the other way round:

function br2nl( $data ) {return preg_replace( "!<br.*>!iU", "\n", $data );}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文