php htmlspecialchars decode 方法 详解 栏目 PHP 繁體版
原文   原文链接

htmlspecialchars() 函数把预约义的字符转换为 HTML 实体。php

预约义的字符是:html

注意:这个函数不能对斜杠/,反斜杠\作处理。编码

示例:code

$content  = '你是/谁啊,大几\都"老梁"作作&>women<a>没<script> alert("hello");</script>';

$content = htmlspecialchars($content);  

// 结果:
你是/谁啊,大几\都&quot;老梁&quot;作作&amp;&gt;women&lt;a&gt;没&lt;script&gt; alert(&quot;hello&quot;);&lt;/script&gt;

// 对反斜杠进行转换
 $content = preg_replace("/\\\/", "&#092;", $content);
//  结果:你是/谁啊,大几&#092;都&quot;老梁

// 对斜杠进行过滤,入库时进行XSS检测攻击。
$content = preg_replace("/\//", "&#47;", $content);

1、HTML 实体

在 HTML 中,某些字符是预留的。
在 HTML 中不能使用小于号(<)和大于号(>),这是由于浏览器会误认为它们是标签。
若是但愿正确地显示预留字符,咱们必须在 HTML 源代码中使用字符实体(character entities)。
字符实体相似这样:
&entity_name;或者entity_number;htm

如需显示小于号,咱们必须这样写:&lt; 或 &#60;

提示:使用实体名而不是数字的好处是,名称易于记忆。不过坏处是,浏览器也许并不支持全部实体名称(对实体数字的支持却很好)。

2、PHP htmlspecialchars() 函数

htmlspecialchars(string,flags,character-set,double_encode)

flags 可选。规定如何处理引号、无效的编码以及使用哪一种文档类型。
可用的引号类型:

character-set:

示例:

$content = "women's life" . '你是/谁啊,大几\都"老梁"作作&>women<a>没<script> alert("hello");</script>';

// 若是使用默认的参数,则不会对单引号进行转换。
$new_str = htmlspecialchars($content );

打印: 
women's life你是/谁啊,大几\都&quot;老梁&quot;作作&amp;&gt;women&lt;a&gt;没&lt;script&gt; alert(&quot;hello&quot;);&lt;/script&gt;

// ENT_QUOTES 编码双引号和单引号。

$new_str = htmlspecialchars($content, ENT_QUOTES);
women&#039;s life你是/谁啊,大几\都&quot;老梁&quot;作作&amp;&gt;women&lt;a&gt;没&lt;script&gt; alert(&quot;hello&quot;);&lt;/script&gt;

3、htmlspecialchars_decode解码

htmlspecialchars_decode(string,flags)

测试:

解码:
$str = ‘women&#039;s life你是/谁啊,大几\都&quot;老梁&quot;作作&amp;&gt;women&lt;a&gt;没&lt;script&gt; alert(&quot;hello&quot;);&lt;/script&gt;’;

// 只解码双引号
$new_str = htmlspecialchars_decode($new_str);
dump($new_str);

打印:
women&#039;s life你是/谁啊,大几\都"老梁"作"作&>women<a>没<script> alert("hello");</script>

// 解码双引号和单引号。
$content = "women's life" . '你是/谁啊,大几\都"老梁"作"作&>women<a>没<script> alert("hello");</script>';

$new_str = htmlspecialchars($content, ENT_QUOTES, gb2312, true);

$new_str = htmlspecialchars_decode($new_str, ENT_QUOTES);
print_r($new_str);

打印:
women's life你是/谁啊,大几\都"老梁"作"作&>women<a>没<script> alert("hello");</script>

4、函数封装

将上边的字符串预约义转为实体封装为一个方法,之后能够直接调用:

$str =  "women's life" . '你是/谁啊,大几\都"老梁"作作&>women<a>没<script> alert("hello");</script>';

// 1.将经常使用的预约义字符转为实体
$new_str = htmlspecialchars($str, ENT_QUOTES, gb2312, true);

// 2.替换反斜杠
 $new_str = preg_replace("/\\\/", "&#092;", $new_str);

// 3.替换斜杠
$content = preg_replace("/\//", "&#47;", $content);

// 打印结果:
women&#039;s life你是&#47;谁啊,大几&#092;都&quot;老梁&quot;作作&amp;&gt;women&lt;a&gt;没&lt;script&gt; alert(&quot;hello&quot;);&lt;&#47;script&gt;

编码-将HTML转为实体

/**
 * 将HTML转为实体
 * @param string $str     须要处理的字符串
 * @param string $charset 编码,默认为gb2312
 * @return string
 */
function html_to_entities($str, $charset = "gb2312")
{
   // 参数判断
    if(empty($str)) return "";
    
    // 1.将经常使用的预约义字符转为实体
    $new_str = htmlspecialchars($str, ENT_QUOTES, $charset);

    // 2.替换反斜杠
    $new_str = preg_replace("/\\\/", "&#092;", $new_str);

    // 3.替换斜杠
    $new_str = preg_replace("/\//", "&#47;", $new_str);
    
    return $new_str;
}

解码-将实体转为HTML

/**
 * 将实体转为HTML
 * @param string $str     须要处理的字符串
 * @return string
 */
function entities_to_html($str)
{
   // 参数判断
    if(empty($str)) return "";
    
    // 1.将实体转为预约义字符
    $new_str = htmlspecialchars_decode($str, ENT_QUOTES);

    // 2.替换反斜杠实体
    $new_str = str_replace("&#092;", "\\", $new_str);

    // 3.替换斜杠实体
    $new_str = str_replace("&#47;", "/", $new_str);
    
    return $new_str;
}

5、小结

通常使用htmlspecialchars将字符串的预约义字符转为实体的时候,须要传递ENT_QUOTES参数,由于若是不传递参数,默认的只对双引号作转换,而单引号不作转换,这样不能起到防止SQL注入的风险,因此,正式用的时候,咱们但愿双引号和单引号及其余可能引发SQL注入的都须要进行实体转换,存入数据库,因此,之后在用这个函数处理的时候,应该传入ENT_QUOTES参数,而后再结合preg_replace方法将斜杠、反斜杠替换为实体,这样就完美了。

相关文章:
HTML实体对照表
入库转HTML为实体的重要性-防SQL注入

相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公众号
   欢迎关注本站公众号,获取更多信息