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注入

相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息