温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php和html标签转换的方法

发布时间:2020-08-15 10:32:16 来源:亿速云 阅读:170 作者:小新 栏目:编程语言

php和html标签转换的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

php html标签转换问题的解决办法:1、使用“htmlentities()”函数将html标签转换成特殊字符;2、使用“html_entity_decode()”函数将htmlentities函数转义过的字符串转成html标签。

php和html标签转换的方法

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

下面分别介绍这两个函数。

1.htmlentities()函数:

说明:将html标签转换成特殊字符。例如将<script>转换成"&lt;script&gt;"

例子:

[PHP] view plaincopy

  1. // An imaginary article submission from a bad user  
  2. //  it will redirect anyone to example.com if the code is run in a browser  
  3. $userInput = "I am going to hax0r your site, hahaha!  
  4.    <script type=&apos;text/javascript&apos;>  
  5.    window.location = &apos;http://www.example.com/&apos;  
  6.    </script>&apos;";  
  7.      
  8. //Lets make it safer before we use it  
  9. $userInputEntities = htmlentities($userInput);  
  10.  
  11. //Now we can display it  
  12. echo $userInputEntities;  

由于最近csdn的控件比较垃圾,请将上面的$apos改成单引号。---呼!

上面的语句执行后,将生成下面的结果

[HTML] view plaincopy

  1. I am going to hax0r your site, hahaha!  
  2.    <script type=&apos;text/javascript&apos;>  
  3.    window.location = &apos;http://www.88web.org/&apos;  
  4.    </script>&apos;  

2.html_entity_decode()函数

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

例子:

[PHP] view plaincopy

  1. $orig = "I&apos;ll /"walk/" the <b>dog</b> now";  
  2.  
  3. $a = htmlentities($orig);  
  4.  
  5. $b = html_entity_decode($a);  
  6.  
  7. echo $a; // I will "walk" the <b>dog</b> now  
  8.  
  9. echo $b; // I will "walk" the <b>dog</b> now  

转载自页面     http://www.cankaojishu.com/bcyy/82144.html

感谢各位的阅读!看完上述内容,你们对php和html标签转换的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI