關於htmlentities 、htmlspecialchars、addslashes的使用

來源:互聯網
上載者:User

標籤:os   ar   使用   sp   strong   資料   on   代碼   html   

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

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

just a test  &amp;  ‘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實體。

預定義字元包括:& (和號) 成為&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():把一些預定義的html實體轉換為字元。

會被解碼的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;


至此,我想大家對著幾個函數的基本試用應經明白了吧。

關於htmlentities 、htmlspecialchars、addslashes的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.