http://www.php.net/manual/en/function.htmlspecialchars.php

用來過濾HTML會用到的特殊字元,將其轉成代碼

瀏覽器在顯示時會變成純文字,而不是HTML CODE

 

string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

The translations performed are:

  • '&' (ampersand) becomes '&'
  • '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
  • "'" (single quote) becomes ''' only when ENT_QUOTES is set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'
要過濾單引號需要再第二個參數加上ENT_QUOTES

 

範例: 

<?php
        $new 
htmlspecialchars("<a href='test'>Test</a>"ENT_QUOTES);
        echo 
$new// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

但是我自己試的結果是有加ENT_QUOTES時,雙引號也會被過濾

<?php
$new = htmlspecialchars('<a href="test">\'Test\'</a>', ENT_QUOTES);
echo $new; // &lt;a href=&quot;test&quot;&gt;&#039;Test&#039;&lt;/a&gt;
?>

結論:要加ENT_QUOTES比較穩當。


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 dizzy03 的頭像
    dizzy03

    碎碎念

    dizzy03 發表在 痞客邦 留言(0) 人氣()