6

我一直试图弄清楚如何在 mysql 表中显示 html 标签。我尝试使用addslashes、mysql_real_escape_string 以及stripslashes 来正确查看标签。每次我通过浏览器查看数据时,它都会显示 html 的文本。例子:

我在 mysql 数据库表中有这个:

<strong>Test</strong>

在网页上查看时应显示如下: 测试

但相反,它显示<strong>Test</strong>

我查看内容的 PHP 代码是:

<?php

require_once("inc.php"); //This includes the configuration for mysql database

?>
<html>
  <head>
  </head>
  <body>
    <p>
<?php

$conn = mysql_connect(HOST,USER,PASS) or die(mysql_error());
        mysql_select_db(NAME) or die(mysql_error());

$sql = "SELECT * FROM events";
$query = mysql_query($sql);

while($row = mysql_fetch_assoc($query)){
  echo(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>
}

mysql_close($conn) or die(mysql_error());

?>
    </p>
  </body>
</html>
4

1 回答 1

43

使用htmlspecialchars_decode

替换以下行

  echo(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>

  echo htmlspecialchars_decode(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>
于 2013-03-01T08:16:29.017 回答