Php nl2br()元';t输出线中断,仅为';n';
这很简单,我的网站上有一个文本区帖子,如果我输入:Php nl2br()元';t输出线中断,仅为';n';,php,Php,这很简单,我的网站上有一个文本区帖子,如果我输入: line 1 line 2 line 3 在其中,它输出: line 1nline 2nline 3 我的插入代码是: $status = strip_tags(stripslashes(htmlentities(mysql_real_escape_string($_POST['status'])))); $uid = strip_tags(stripslashes(htmlentities(mysql_real_escap
line 1
line 2
line 3
在其中,它输出:
line 1nline 2nline 3
我的插入代码是:
$status = strip_tags(stripslashes(htmlentities(mysql_real_escape_string($_POST['status']))));
$uid = strip_tags(stripslashes(htmlentities(mysql_real_escape_string($_POST['uid']))));
//more stuff
$sid = rndTxt(16);
$status = nl2br($status);
if (!get_magic_quotes_gpc()) {
$status = addslashes($status);
}
$insert = mysql_query("INSERT INTO mingle_status (uid,sid,status,`timestamp`) VALUES ('$uid','$sid','$status',now())") or
print mysql_error();
和我的输出代码:
while($st = mysql_fetch_assoc($statussql)) {
$status = stripslashes($st['status']);
$sid = $st['sid'];
$td = $st['timestamp'];
?>
<div id="n">
<div id="statuses" class="<?php echo $sid; ?>">
<p><?php echo $status; ?></p>
<div id="statuscomadd" style="background:#E0E0E0;">
<a href="ld.php?uid=<?php $uid; ?>&pid=<?php echo $sid;?>&method=like">Like</a> <a href="ld.php?uid=<?php $uid; ?>&pid=<?php echo $sid;?>&method=dislike">Dislike</a><a href="#" id="time"><?php echo time_since($td) . " ago"; ?></a>
</div>
</div>
while($st=mysql\u fetch\u assoc($statussql)){
$status=stripslashes($st['status']);
$sid=$st['sid'];
$td=$st['timestamp'];
?>
插入时不需要使用nl2br()
,在html中显示时必须使用它
并且在插入之前必须删除stripslashes
您不需要在插入时使用nl2br()
,在html中显示时必须使用它
并且在插入之前必须删除stripslashes
您使用strip\u标记(stripslashes(htmlentities(mysql\u real\u escape\u string());
从\n
中删除斜线
只需对HTML使用mysql\u real\u escape\u string()
,或htmlentities(,ENT\u引号)
此外,如果可能,使用UTF-8编码和htmlspecialchars()
而不是htmlspecialchars()
htmlspecialchars()
转换具有HTML表示形式的每个字符,而htmlspecialchars()
只转换必要的字符。无需转换所有字符。请参阅:您使用带标记(带斜杠(htmlentities(mysql\u real\u escape\u string());
从\n
中带斜杠
只需对HTML使用mysql\u real\u escape\u string()
,或htmlentities(,ENT\u引号)
此外,如果可能,使用UTF-8编码和htmlspecialchars()
而不是htmlspecialchars()
htmlspecialchars()
转换具有HTML表示形式的每个字符,而htmlspecialchars()
只转换必要的字符。无需转换所有字符。请参阅:插入时,只需对值执行mysql\u real\u escape\u string()
即可。您只需更改数据(例如,在显示数据时使用htmlentities()
)
也请考虑停止使用<代码> MySqL**/Cudio>新代码的功能。它们不再被维护,社区已经开始了。看到了。相反,你应该学习和使用其中的一个或。如果你不能决定,将有助于选择。如果你愿意学习,
另一件事:你真的需要htmlspecialchars()
?因为在我看来,更好的解决方案是使用htmlspecialchars()
。否则所有html实体都将被替换
另外,我认为您不需要使用strip_tags()
,因为您已经在使用htmlspecialchars()
来保护您免受XSS攻击
现在对你来说,问题是因为你正在使用stripslashes()
,这会破坏\n
换行符。我认为你可以在插入时删除那些add/stripslashes
只要对值执行mysql\u real\u escape\u string()
。你只想更改数据(例如,通过使用htmlentities()
当您要显示它时)
也请考虑停止使用<代码> MySqL**/Cudio>新代码的功能。它们不再被维护,社区已经开始了。看到了。相反,你应该学习和使用其中的一个或。如果你不能决定,将有助于选择。如果你愿意学习,
另一件事:你真的需要htmlspecialchars()
?因为在我看来,更好的解决方案是使用htmlspecialchars()
。否则所有html实体都将被替换
另外,我认为您不需要使用strip_tags()
,因为您已经在使用htmlspecialchars()
来保护您免受XSS攻击
现在对你来说,问题是因为你使用的是stripslashes()
,它打破了\n
换行符。我想你可以去掉那些add/stripslashes
这是你的组合:strip\u标签(stripslashes)(htmlentities(mysql\u real\u escape\u string(
,作为一般指导原则,在将内容插入数据库时调用mysql\u real\u escape\u string
,只有在输出html时才调用htmlentities
。您需要使用mysql\u real\u escape\u string
来转义您的输入-addslashes不会剪切它可能在stripsslashes()之前使用nl2br()
)
以及在上述粘贴代码的前两行上发生的所有其他事情。您的目的是用
替换换行符还是维护换行符?n您得到的东西看起来像是来自stripslashes
,其中\n被转换为简单的n。编辑:我建议不要使用换行符,当您想要以html格式输出它时,使用ln2br,而不是将它插入到数据库中。您很容易受到SQL注入的攻击,因为您也在调用stripslashes。因此,这一大块代码不起任何作用除了咀嚼你的输入之外,你还有一些组合:strip\u标记(stripslashes(htmlentities)(mysql\u real\u escape\u string(
,作为一般指导原则,在将内容插入数据库时调用mysql\u real\u escape\u string
,只有在输出html时才调用htmlentities
。您需要使用mysql\u real\u escape\u string
来转义您的输入-addslashes不会剪切它可能在stripsslashes()之前使用nl2br()
)
以及在上述粘贴代码的前两行上发生的所有其他事情。您的目的是用
替换换行符还是维护换行符?n您得到的东西看起来像是来自stripslashes
,其中\n被转换为简单的n。编辑:我建议不要使用换行符,当您想要以html格式输出它时,使用ln2br,而不是将它插入到数据库中。您很容易受到SQL注入的攻击,因为您也在调用stripslashes。因此,这一大块代码不起任何作用除了咀嚼你的输入之外。为什么htmlentities()