1、html_entity_decode():把html實體轉換為字符。
Eg:$str = "just atest & 'learn to use '";echo html_entity_decode($str);echo "<br />";echo html_entity_decode($str,ENT_QUOTES);echo "<br />";echo html_entity_decode($str,ENT_NOQUOTES);
輸出如下:
just a test & 'learn to use 'just a test & 'learn to use 'just a test & 'learn to use '
2、htmlentities():把字符轉換為html實體。
Eg:$str = "just a test & 'learn to use'"; echo htmlentities($str,ENT_COMPAT); echo "<br/>"; echo htmlentities($str, ENT_QUOTES); echo "<br/>"; echo htmlentities($str, ENT_NOQUOTES);
輸出如下:
just a test & 'learn to use'just a test & 'learn to use'just a test & 'learn to use'
查看源代碼如下:
just a test & 'learn to use'<br />just a test & 'learn to use'<br />just a test & 'learn to use'
3、addslashes():在指定的預定義字符前添加反斜杠
預定義字符包括:單引號(‘),雙引號(“),反斜杠(/),NULL
默認情況下,PHP指令 magic_quotes_gpc 為 on,對所有的GET、POST 和COOKIE 數據自動運行 addslashes()。不要對已經被 magic_quotes_gpc 轉義過的字符串使用 addslashes(),因為這樣會導致雙層轉義。遇到這種情況時可以使用函數get_magic_quotes_gpc() 進行檢測。
Eg:$str3="/ just a ' /" test";echoaddslashes($str3);
輸出:
// just a /' /" test
4、stripslashes():刪除由addslashes函數添加的反斜杠
Eg:$str4="// just a /'/" test";echo stripslashes($str4);
輸出:
just a ' " test
5、 htmlspecialchars():把一些預定義的字符轉換為html實體。
預定義字符包括:& (和號) 成為& " (雙引號) 成為"' (單引號) 成為'< (小于) 成為<> (大于) 成為>Eg:$str5 = "just atest & 'learn to use'";echo htmlspecialchars($str5, ENT_COMPAT);echo "<br/>";echo htmlspecialchars($str5, ENT_QUOTES);echo "<br/>";echo htmlspecialchars($str5, ENT_NOQUOTES);
輸出:
just a test & 'learn to use'just a test & 'learn to use'just a test & 'learn to use'
查看源代碼:
just a test & 'learn to use'<br />just a test & 'learn to use'<br />just a test & 'learn to use'
6、 htmlspecialchars_decode():把一些預定義的html實體轉換為字符。
會被解碼的html實體包括:& 成為 &(和號)
" 成為 " (雙引號)
' 成為 ' (單引號)
< 成為 < (小于)
> 成為 > (大于)
Eg:$str6 = "just atest & 'learn to use'";echo htmlspecialchars_decode($str6);echo "<br />";echo htmlspecialchars_decode($str6, ENT_QUOTES);echo "<br />";echo htmlspecialchars_decode($str6, ENT_NOQUOTES);
輸出:
just a test & 'learn to use 'just a test & 'learn to use 'just a test & 'learn to use '
查看源代碼:
just a test & 'learn to use '<br />just a test & 'learn to use '<br />just a test & 'learn to use '
防注入防web腳本綜合使用:
$str= htmlspecialchars(addslashes($str));$str= htmlspecialchars_decode(stripslashes($str));
以上這篇淺談htmlentities 、htmlspecialchars、addslashes的使用方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選