我正在尝试学习PHP + mysql,当保存数据到我的数据库时PHP不会回显以“<”开头的行符号
截图中的mysql,
下的代码 $result2 = mysqli_query($con, "SELECT * FROM ta_pastat pastat left join ta_users tusers on pastat.kirjoittaja_ID = tusers.ID where pastat.kategoria_ID = 1");
while($row2 = mysqli_fetch_array($result2)){
//$row2 = preg_match('(^|>)(>[^<]*)', "\\1<div class=\"unkfunc\">\\2</div>", $row2);
$style = "";
switch( substr($row2['Teksti'], 0, 1) ) {
case ">":
$style = "style=\"color:#789922\"";
break;
case "<":
$style = "style=\"color:#226199\"";
break;
}
//echo nl2br("<h4 $style>".$row2['Teksti']."</h4><br>".PHP_EOL);
echo nl2br("<br><h1>".$row2['Otsikko']."</h1><br><h4 $style>".$row2['Teksti']."</h4><hr>");
答案 0 :(得分:3)
由于浏览器将其解释为HTML,因此您需要将其包装在htmlentities
中。
echo nl2br("<br><h1>".$row2['Otsikko']."</h1><br><h4 $style>".htmlentities($row2['Teksti'])."</h4><hr>");