打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于在php中html标签的转换问题的解决
很多朋友在写php的时候,难免会遇到需要将html标签进行转义存储。比如存入数据库、xml文件等。而存储进去后,读取出来则需要转换成html输出。我也是找了两天,才找到解决方法的。
下面分别介绍这两个函数。
1.htmlentities()函数:
说明:将html标签转换成特殊字符。例如将<script>转换成"&lt;script&gt;"
例子:
[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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
PHP安全编程:对输出要进行转义
跨站脚本漏洞(XSS)基础讲解
编程语言php中htmlspecialchars、strip_tags、addslashes函数的简单介绍
XSS绕过学习
PHP+Tidy-完美的XHTML纠错+过滤
php htmlentities和htmlspecialchars 的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服