关于在php中html标签的转换问题的解决,转义与反转义

1 篇文章 0 订阅
1 篇文章 0 订阅

很多朋友在写PHP的时候,难免会遇到需要将html标签进行转义存储。比如存入数据库、xml文件等。而存储进去后,读取出来则需要转换成html输出。网上有许多人编写的转换函数,很长很难懂。其实php早就自带有这样的函数。大可不必自己编写。
下面分别介绍这两个函数。

1.htmlentities()函数:
说明:将html标签转换成特殊字符。例如将

[PHP] view plaincopy
// An imaginary article submission from a bad user  
//  it will redirect anyone to example.com if the code is run in a browser  
$userInput = "I am going to hax0r your site, hahaha!  
    <script type=&apos;text/javascript&apos;>  
    window.location = &apos;http://www.example.com/&apos;  
    </script>&apos;";  

//Lets make it safer before we use it  
$userInputEntities = htmlentities($userInput);  

//Now we can display it  
echo $userInputEntities;  

由于最近csdn的控件比较垃圾,请将上面的$apos改成单引号。—呼!
上面的语句执行后,将生成下面的结果

[HTML] view plaincopy
I am going to hax0r your site, hahaha!  
    <script type=&apos;text/javascript&apos;>  
    window.location = &apos;http://www.88web.org/&apos;  
    </script>&apos;  

2.html_entity_decode()函数
说明:将htmlentities()函数转义过的字符串转成html标签。
例子:

[PHP] view plaincopy
$orig = "I&apos;ll /"walk/" the <b>dog</b> now";  

$a = htmlentities($orig);  

$b = html_entity_decode($a);  

echo $a; // I will "walk" the <b>dog</b> now  

echo $b; // I will "walk" the <b>dog</b> now  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值