我无法从数据库中获取数据并在 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!
发布评论
评论(5)
使用正确的 HTML/CSS。
;-)
换行符在 HTML pre 标记中或在 CSS 空白属性设置为:
资源的标记中中断所有工作:
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:
Resources:
http://www.w3schools.com/cssref/pr_text_white-space.asp
http://www.quirksmode.org/css/whitespace.html
首先,您不应该使用 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 dostr_replace('<br>',"\n",$string);
which will convert the<br>
's into\n
's这对我有用:
This worked for me:
对于很长的行,您还需要文本换行,而不是可能脱离其父容器。要覆盖这种情况,同时保留新行,请使用以下 css:
资源: 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:
resource: https://css-tricks.com/almanac/properties/w/whitespace/
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 );}