欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談htmlentities 、htmlspecialchars、addslashes的使用方法

 更新時間:2016年12月09日 10:01:34   投稿:jingxian  
下面小編就為大家?guī)硪黄獪\談htmlentities 、htmlspecialchars、addslashes的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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 &amp; 'learn to use'<br />

just a test &amp; &#039;learn to use&#039;<br />

just a test &amp; '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ù)定義字符包括:& (和號) 成為&amp;  
 " (雙引號) 成為&quot;
' (單引號) 成為&#039;
< (小于) 成為&lt;
> (大于) 成為&gt;

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 &amp; 'learn to use'<br />
just a test &amp; &#039;learn to use&#039;<br />
just a test &amp; 'learn to use'

6、 htmlspecialchars_decode():把一些預(yù)定義的html實體轉(zhuǎn)換為字符。

會被解碼的html實體包括:&amp; 成為 &(和號)

 &quot; 成為 " (雙引號)
 &#039; 成為 ' (單引號)
 &lt; 成為 < (小于)
 &gt; 成為 > (大于)

Eg:$str6 = "just atest &amp; &#039;learn to use&#039;";

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 & &#039;learn to use &#039;<br />

just a test & 'learn to use '<br />

just a test & &#039;learn to use &#039;

防注入防web腳本綜合使用:

$str= htmlspecialchars(addslashes($str));

$str= htmlspecialchars_decode(stripslashes($str));

以上這篇淺談htmlentities 、htmlspecialchars、addslashes的使用方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • php array_intersect()函數(shù)使用代碼

    php array_intersect()函數(shù)使用代碼

    array_intersect() 返回一個數(shù)組,該數(shù)組包含了所有在 array1 中也同時出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意鍵名保留不變。
    2009-01-01
  • PHP中source #N問題的解決方法

    PHP中source #N問題的解決方法

    最近寫PHP里面的查詢經(jīng)常會遇到source #4或者source#5這樣的問題,下面有個不錯的解決方法,大家可以嘗試下
    2014-01-01
  • 用PHP讀取和編寫XML DOM的實現(xiàn)代碼

    用PHP讀取和編寫XML DOM的實現(xiàn)代碼

    有許多技術(shù)可用于用 PHP 讀取和編寫 XML。本文提供了三種方法讀取 XML:使用 DOM 庫、使用 SAX 解析器和使用正則表達(dá)式。還介紹了使用 DOM 和 PHP 文本模板編寫 XML。
    2011-02-02
  • SSI指令

    SSI指令

    這篇文章主要介紹了SSI指令,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2006-11-11
  • smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法

    smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法

    這篇文章主要介紹了smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法,涉及config_load載入配置文件及相關(guān)的讀取技巧,需要的朋友可以參考下
    2015-01-01
  • fleaphp常用方法分頁之Pager使用方法

    fleaphp常用方法分頁之Pager使用方法

    fleaphp常用方法分頁之Pager使用方法,需要的朋友可以參考下。
    2011-04-04
  • 屏蔽PHP默認(rèn)設(shè)置中的Notice警告的方法

    屏蔽PHP默認(rèn)設(shè)置中的Notice警告的方法

    很多時候其實寫出來的代碼的錯誤可以忽略或者根本就不是錯誤,PHP還是會顯示Notice警告,well接下來我們就來介紹一下屏蔽PHP默認(rèn)設(shè)置中的Notice警告的方法
    2016-05-05
  • PHP數(shù)組在底層的實現(xiàn)原理詳解

    PHP數(shù)組在底層的實現(xiàn)原理詳解

    這篇文章講給大家詳細(xì)介紹一下PHP數(shù)組在底層的實現(xiàn)原理,PHP數(shù)組在底層的實現(xiàn)原理可以分為兩種類型:基于哈希表的實現(xiàn)和基于有序列表的實現(xiàn),文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下
    2023-11-11
  • php微信開發(fā)自定義菜單

    php微信開發(fā)自定義菜單

    這篇文章主要為大家詳細(xì)介紹了php微信開發(fā)自定義菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡單用法示例

    php數(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

最新評論