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

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

1.htmlentities()函数:

说明:将html标签转换成特殊字符。例如将<script>转换成”<script>”

例子:

[PHP]
  1. // An imaginary article submission from bad user  
  2. //  it will redirect anyone to example.com if the code is run in browser  
  3. $userInput “I am going to hax0r your site, hahaha!  
  4.     <script type='text/javascript'>  
  5.     window.location 'http://www.example.com/'  
  6.     </script>'”;  
  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]
  1. am going to hax0r your site, hahaha!  
  2.     <script type='text/javascript'>  
  3.     window.location 'http://www.88web.org/'  
  4.     </script>'  

2.html_entity_decode()函数

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

例子:

[PHP]
  1. $orig “I'll /”walk/” the <b>dog</b> now”;  
  2.   
  3. $a htmlentities($orig);  
  4.   
  5. $b html_entity_decode($a);  
  6.   
  7. echo $a; // will “walk” the <b>dog</b> now  
  8.   
  9. echo $b; // will “walk” the <b>dog</b> now 

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

php实现redis延时队列

写了一个简单的类实现 使用方法 $class = new RedisDelayQueue(); $class->execute(); <?php namespace app\common\service; class RedisDelay { ...

阅读全文

php获取包含10w个手机号的txt文件(逗号隔开)

txt文件格式如下 13000000000,13000000005,13000000403,13001010129,13001010688,13001010838,13001016389,13001019538,13001030005,13001042700,13001047495...

阅读全文

欢迎留言