如何解决php html标签转换问题

发布时间:2024-11-08 点击:135
php html标签转换问题的解决办法:1、使用“htmlentities()”函数将html标签转换成特殊字符;2、使用“html_entity_decode()”函数将htmlentities函数转义过的字符串转成html标签。
推荐:《php视频教程》
很多朋友在写php的时候,难免会遇到需要将html标签进行转义存储。比如存入数据库、xml文件等。而存储进去后,读取出来则需要转换成html输出。网上有许多人编写的转换函数,很长很难懂。其实php早就自带有这样的函数。大可不必自己编写。
下面分别介绍这两个函数。
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
转载自页面 http://www.cankaojishu.com/bcyy/82144.html

360云服务器怎么登入
腾讯云服务器买什么套餐划算啊
解除主断查看效果-云服务器问题
阿里云服务器ecs备案号
企业邮箱无法使用-企业邮局
没有个体户执照能办理商标吗
默认首页设置-虚拟主机/数据库问题
什么叫云虚拟主机