简体   繁体   English

Textareas,New Lines和nl2br的问题

[英]Problems with Textareas, New Lines and nl2br

I'm having trouble getting data out of the database and echoing out in a HTML page textarea. 我无法从数据库中获取数据并在HTML页面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. 除了数据库中的链接和存储之外,这只是简单地剥离HTML标记。 If you look in the database, the line breaks are invisible but there, so i assume they are \\n and \\r. 如果你查看数据库,换行符是不可见的,但我认为它们是\\ n和\\ r \\ n。

If i were to type into a textarea: 如果我要输入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: 当回显到textarea时,这就是显示的内容:

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? 如果我删除mysqli_real_escape_string,换行符会被保留并完美运行,我是否必须为此牺牲安全性?

SOLVED: 解决了:

mysqli_real_escape_string causing the problem, do not echo out a variable which has had this applied. 导致问题的mysqli_real_escape_string,不会回显已应用此变量的变量。 Only use mysqli_real_escape_string when inserting, deleting, etc from a database, not before, definitely not after ;) 从数据库插入,删除等时只使用mysqli_real_escape_string,而不是之前,绝对不是之后;)

Thanks everyone! 感谢大家!

Use the correct HTML/CSS. 使用正确的HTML / CSS。

;-) ;-)

The line breaks all work in an HTML pre tag, or in a tag with the CSS white-space property set to: 该行断开HTML预标记中的所有工作,或者将CSS空白属性设置为的标记:

white-space: pre;

Resources: 资源:

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

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

This worked for me: 这对我有用:

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

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: 要覆盖这种情况,还要保留新行,请使用以下css:

white-space: pre-wrap;

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

Firstly, you shouldn't be using nl2br as this will change your \\n 's to <br> 's. 首先,您不应该使用nl2br,因为这会将您的\\n更改为<br>

You will most likely need to strip the slashes echo stripslashes($_SESSION['content']) . 你很可能需要echo stripslashes($_SESSION['content'])斜线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); 如果数据存储为数据库中的<br> ,则可以执行str_replace('<br>',"\\n",$string); which will convert the <br> 's into \\n 's 这会将<br>转换成\\n

The nl2br function is working with that content as a string: http://codepad.org/M2Jw9KQJ nl2br函数正在将该内容用作字符串: 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 );} function br2nl($ data){return preg_replace(“!<br。*>!iU”,“\\ n”,$ data);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM