我在课堂上有一个功能:
public function displayComments($postorpage, $id_postorpage){
$query = mysql_query("SELECT * FROM `".DB."`.`mmsv_servers_page_comments` WHERE `id_postorpage`='$id_postorpage'");
while($comment = mysql_fetch_object($query)){
$added = new Time;
$elapsed = $added->displayElapsedSignificant($comment->data);
echo "
<h3>$comment->nick</h3>
<i>$elapsed</i>
<p>".addSecurityView($comment->text)."</p>
";
}
}
假设删除断行的函数addSecurityView不起作用:
function addSecurityView($value)
{
$result = preg_replace(array("/\r\n\r\n/", "/\n\n/"), array("\r\n", "\n"), $value);
return $result;
}
答案 0 :(得分:0)
尝试使用strtr函数,替换对。 http://php.net/manual/en/function.strtr.php
答案 1 :(得分:0)
在这里试试这个
$string = trim(preg_replace('/\s+/', ' ', $string));
这里有简单的SECURE输出功能
function secureOutput($string) {
$string = trim(preg_replace('/\s+/', ' ', $string)); //this will remove multiple whitespaces
return htmlentities($string, ENT_QUOTES);
}
答案 2 :(得分:0)
我相信这应该适合你,因为它与我合作
<?php
$trans = array("\r" => "" , "\n" => "");
echo strtr("\n hi and \r hi", $trans);
?>
并考虑从MySql更改为MySqli或PDO