htmlspecialchars()函数和html_entity_decode()函数

本文介绍如何使用CKEditor进行富文本编辑,并通过htmlspecialchars将编辑内容转换为HTML实体存储到数据库中,再利用html_entity_decode还原显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

test.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Full Page Editing - CKEditor Sample</title>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
 <h1>
  CKEditor Sample
 </h1>
  <form action="posteddata.php" method="post">
   <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
   <script type="text/javascript">
    CKEDITOR.replace( 'editor1' );

   </script>
  <p>
   <input type="submit" value="Submit" />
  </p>
 </form>
</body>
</html>

 

posteddata.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<?php
 require_once('db_conn.php');
 
 if(!$db_conn)
 {
  die('Could not connect: ' . mysql_error());
  exit();
 }

if ( isset( $_POST ) )
 $postArray = &$_POST ;   
else
 $postArray = &$HTTP_POST_VARS ; 

foreach ( $postArray as $sForm => $value )
{
 if ( get_magic_quotes_gpc() )
  $postedValue = htmlspecialchars( stripslashes( $value ) ) ;
 else
  $postedValue = htmlspecialchars( $value ) ;
  
}
  
 mysql_select_db($db_name, $db_conn);
  
 $query="INSERT INTO $tbl_test (content) values ('$postedValue')";

 $result = mysql_query($query,$db_conn);
  if($result)
  {
   echo "successful";
  }
  else
  {
   echo "ERROR";
  }
 
  mysql_close($db_conn);
?>
</body>
</html>

 

show.php:

<?php
 require_once('db_conn.php');
 
 if(!$db_conn)
 {
  die('Could not connect: ' . mysql_error());
  exit();
 }
  
 mysql_select_db($db_name, $db_conn);
  
 $query="SELECT * FROM $tbl_test ORDER BY contentid DESC LIMIT 0,1";

 $result = mysql_query($query,$db_conn);
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<? 
 $rows=mysql_fetch_array($result);
 echo html_entity_decode($rows['content']);
?>
</body>
</html>
<? 
  mysql_close($db_conn);
?>

 

StripSlashes()函数:本函数可去掉字符串中的反斜线字符。若是连续二个反斜线,则去掉一个,留下一个。若只有一个反斜线,就直接去掉。

addslashes()函数:返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(/)与 NUL(NULL 字符)。

htmlspecialchars()函数:把一些预定义的字符转换为 HTML 实体。

html_entity_decode()函数:把HTML实体转换成一些预定义的字符。

使用CKEditor进行HTML编辑,使用htmlspecialchars()函数把字符转换成HTML实体存入数据库,从数据库提取数据时使用html_entity_decode()函数把HTML实体转换成字符.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值