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