【问题标题】:How to replace \r\n with a line break [duplicate]如何用换行符替换 \r\n [重复]
【发布时间】:2013-01-30 17:48:15
【问题描述】:

我在 PHP 中有一个注释系统,当用户在 textarea 中键入换行符时,它显示为 rn(注意:我正在清理此输入并使用 htmlentities(),并且我有自定义标记) .

这是我当前的代码(包括换行符的尝试):

$comment_content =stripslashes(str_replace('\r\n', '@//', mysql_real_escape_string($_POST['comment_content'])));
$comment_content = htmlentities($comment_content);
$comment_content = mysql_real_escape_string(str_replace("====", "<span class=".$bold.">", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("===", "</span>", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("~~~", "<span class=".$italic.">", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("~~", "</span>", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("++++", "<span class=".$big.">", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("+++", "</span>", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("___", "<span class=".$underline.">", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("__", "</span>", $comment_content));
$comment_content = mysql_real_escape_string(str_replace("@//", "<br>", $comment_content));
$comment_content = comment_sanitize($comment_content);

这就是我的消毒方式:

function sanitize($sql, $formUse = true) {
    $sql = preg_replace("/(from|script|src|select|insert|delete|where|drop table|show tables|`|,|'|\*|\\\\)/i","",$sql);
    $sql = trim($sql);
    if(!$formUse || !get_magic_quotes_gpc()) {
        $sql = addslashes($sql);
    }
    return $sql;
}

有什么想法吗?

【问题讨论】:

  • 你在做什么来清理输入?试试……做点别的。
  • PHP 的原生函数nl2br() 是又快又脏的方式。
  • Wooble,我正在使用自定义清理代码。 ceejayoz,谢谢。 j08691,好的,会的。

标签: php


【解决方案1】:

您不要在输入时使用htmlentities,而是在输出时使用它。您应该转义以输入数据库,然后在输出中显示换行符考虑:

nl2br(htmlentities($comment));

【讨论】:

  • 嗯,看,问题是,我对这个评论系统有自己的标记,它把~~~Underline Text~~之类的东西改成了下划线文本。我稍后会发布我的代码,以便您了解我的意思。
  • @Zbee 如果您先执行 nl2br/htmlentities,您仍然可以进行所有替换,并且在输出之前它们不会被转义。
  • 因此,如果我的数据库中有 Span-ness 作为评论并将该评论回显为 htmlentitites($comment) 它只会在 标签?
  • @Zbee 我建议在 SELECT 之后而不是在 INSERT 之前进行替换。这样你可以在以后调整你的样式而不会感到头疼
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2019-09-19
  • 2016-02-06
  • 2011-05-12
  • 1970-01-01
相关资源
最近更新 更多