简体   繁体   English

nl2br()不会输出换行符,只是'n'

[英]nl2br() won't output line breaks, just 'n'

It's pretty simple, I have a text area post on my website, and if I input: 这很简单,我的网站上有一个文本区域帖子,如果输入:

line 1
line 2
line 3

into it, it outputs: 进入它,输出:

line 1nline 2nline 3

My insert code is: 我的插入代码是:

    $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();

and my output code: 和我的输出代码:

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>

Any help would be greatly appreciated!:) 任何帮助将不胜感激!:)

you dont need to use nl2br() on insert, you will have to use it while displaying in html 您不需要在插入时使用nl2br() ,则在html中显示时必须使用它

and will have to remove stripslashes before insert 并且必须在插入前去除长条stripslashes

When inserting just do a mysql_real_escape_string() over the values. 插入时,只需对值进行mysql_real_escape_string() You only want to change the data (eg by using htmlentities() when you are going to display it). 您只想更改数据(例如,在显示数据时使用htmlentities() )。

Please also consider to stop using mysql_* functions for new code. 还请考虑停止将mysql_*函数用于新代码。 They are no longer maintained and the community has begun the deprecation process . 它们已不再维护,社区已开始弃用过程 See the red box ? 看到红色框了吗? Instead you should learn about prepared statements and use either PDO or MySQLi . 相反,您应该了解准备好的语句,并使用PDOMySQLi If you can't decide, this article will help to choose. 如果您不能决定, 本文将有助于选择。 If you care to learn, here is a good PDO tutorial . 如果您想学习, 这里有个不错的PDO教程

Another thing: do you realy need htmlentities() ? 另一件事:您真的需要htmlentities()吗? Because imo a better solution is to use htmlspecialchars() . 因为imo一个更好的解决方案是使用htmlspecialchars() Otherwise all html entities will be replaced. 否则, 所有 html实体将被替换。

Also I don't think you need to use strip_tags() , because you are already doing htmlspecialchars() to protect you against XSS. 我也不认为您需要使用strip_tags() ,因为您已经在执行htmlspecialchars()来保护自己免受XSS攻击。

Now for you problem is it because you are using stripslashes() which breaks the \\n linebreaks. 现在对您来说问题是因为您正在使用stripslashes()破坏\\n换行符。 I think you can just drop those add/stripslashes . 我认为您可以删除那些add/stripslashes

You use strip_tags(stripslashes(htmlentities(mysql_real_escape_string()))); 您使用strip_tags(stripslashes(htmlentities(mysql_real_escape_string()))); which strips the slashes from \\n . \\n去除斜线。

Just use mysql_real_escape_string() , or htmlentities( ,ENT_QUOTES) for HTML. 只需将mysql_real_escape_string()htmlentities( ,ENT_QUOTES)用于HTML。

Also, if it's possible use an UTF-8 encoding and htmlspecialchars() instead of htmlentities() . 另外,如果可能,请使用UTF-8编码和htmlspecialchars()而不是htmlentities() htmlentities() converts every character which has an HTML-representation, while htmlspecialchars() converts only the necessary characters. htmlentities()转换具有HTML表示形式的每个字符,而htmlspecialchars()仅转换必需的字符。 There's no need to convert everything. 无需转换所有内容。 See: htmlentities vs htmlspecialchars 请参阅: htmlentities vs htmlspecialchars

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

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