淺談htmlentities 、htmlspecialchars、addslashes的使用方法
1、html_entity_decode():把html實體轉(zhuǎn)換為字符。
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():把字符轉(zhuǎn)換為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():在指定的預(yù)定義字符前添加反斜杠
預(yù)定義字符包括:單引號(‘),雙引號(“),反斜杠(\),NULL
默認(rèn)情況下,PHP指令 magic_quotes_gpc 為 on,對所有的GET、POST 和COOKIE 數(shù)據(jù)自動運行 addslashes()。不要對已經(jīng)被 magic_quotes_gpc 轉(zhuǎn)義過的字符串使用 addslashes(),因為這樣會導(dǎo)致雙層轉(zhuǎn)義。遇到這種情況時可以使用函數(shù)get_magic_quotes_gpc() 進(jìn)行檢測。
Eg:$str3="\ just a ' \" test"; echoaddslashes($str3);
輸出:
\\ just a \' \" test
4、stripslashes():刪除由addslashes函數(shù)添加的反斜杠
Eg:$str4="\\ just a \'\" test"; echo stripslashes($str4);
輸出:
just a ' " test
5、 htmlspecialchars():把一些預(yù)定義的字符轉(zhuǎn)換為html實體。
預(yù)定義字符包括:& (和號) 成為& " (雙引號) 成為" ' (單引號) 成為' < (小于) 成為< > (大于) 成為> 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():把一些預(yù)定義的html實體轉(zhuǎn)換為字符。
會被解碼的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的使用方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php array_intersect()函數(shù)使用代碼
array_intersect() 返回一個數(shù)組,該數(shù)組包含了所有在 array1 中也同時出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意鍵名保留不變。2009-01-01smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法
這篇文章主要介紹了smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法,涉及config_load載入配置文件及相關(guān)的讀取技巧,需要的朋友可以參考下2015-01-01屏蔽PHP默認(rèn)設(shè)置中的Notice警告的方法
很多時候其實寫出來的代碼的錯誤可以忽略或者根本就不是錯誤,PHP還是會顯示Notice警告,well接下來我們就來介紹一下屏蔽PHP默認(rèn)設(shè)置中的Notice警告的方法2016-05-05php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡單用法示例
這篇文章主要介紹了php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡單用法,結(jié)合實例形式分析了PHP數(shù)組函數(shù)array_push()、array_pop()及array_shift()操作數(shù)組的入棧、出棧、移除等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2020-01-01