php中我講到的安全函數有htmlentities() 和htmlspecialchars() html_entity_decode()和htmlspecialchars_decode() addslashes()和addcslashes()這幾個函數了,下面本文章重點介紹一下這6個函數.
一、htmlentities() 和htmlspecialchars()
1、htmlentities()
1.1 功能:把字符轉換為 HTML 實體,字符包括ASCII實體和ISO 8859-1實體(HTML實體對照表:http://www.w3school.com.cn/tags/html_ref_entities.html)
1.2 語法:htmlentities(string,quotestyle,character-set)
1.3 參數:string是必選參數,是需要轉換的字符串,其余可選,quotestyle規定如何編碼單引號和雙引號:ENT_COMPAT – 默認,僅編碼雙引號;ENT_QUOTES – 編碼雙引號和單引號;ENT_NOQUOTES – 不編碼任何引號,character-set是規定轉換用的字符集,常用的有UTF-8/GB-2312/ISO-8859-1(默認).
1.4 提示:無法被識別的字符集將被忽略,并由 ISO-8859-1 代替.
- $str = "John & 'Adams'";
- echo htmlentities($str);
- //在瀏覽器中輸出:John & 'Adams'
- //查看源代碼:John & 'Adams'
2、htmlspecialchars()
2.1 把一些預定義的字符轉換為 HTML 實體,預定義字符都是ASCII 實體,即此函數不能轉換ISO 8859-1實體,這是和htmlrntities()的區別.
預定義的字符是:
- & (和號) 成為 &
- ” (雙引號) 成為 "
- ‘ (單引號) 成為 '
- < (小于) 成為 <
- > (大于) 成為 >
2.2 htmlspecialchars(string,quotestyle,character-set)
2.3 參數htmlentities()
2.4 提示:無法被識別的字符集將被忽略,并由 ISO-8859-1 代替.
- $str = "John & 'Adams'";
- echo htmlentities($str);
- //在瀏覽器中輸出:John & 'Adams' Vevb.com
- //查看源代碼:John & 'Adams'
二、html_entity_decode()和htmlspecialchars_decode()
html_entity_decode(string,quotestyle,character-set) 函數把 HTML 實體轉換為字符,是htmlentities()的.
新聞熱點
疑難解答