我在PHP中混淆的三組函數總結(htmlentities,html_entity_decode,addslashes)

來源:互聯網
上載者:User

一、htmlentities() 和htmlspecialchars()

1、htmlentities()

1.1  功能:把字元轉換為 HTML 實體。字元包括ASCII實體和ISO 8859-1實體(HTML實體對照表:http://www.w3school.com.cn/tags/html_ref_entities.html)

1.2  文法:htmlentities(string,quotestyle,character-set)

1.3  參數:string是必選參數,是需要轉換的字串。其餘可選,quotestyle規定如何編碼單引號和雙引號:ENT_COMPAT – 預設。僅編碼雙引號;ENT_QUOTES – 編碼雙引號和單引號;ENT_NOQUOTES – 不編碼任何引號。character-set是規定轉換用的字元集,常用的有UTF-8/GB-2312/ISO-8859-1(預設)。

1.4  提示:無法被識別的字元集將被忽略,並由 ISO-8859-1 代替。

$str = "John & 'Adams'";
echo htmlentities($str);
//在瀏覽器中輸出:John & 'Adams'
//查看原始碼:John & 'Adams'

2、htmlspecialchars()

2.1   把一些預定義的字元轉換為 HTML 實體。預定義字元都是ASCII 實體,即此函數不能轉換ISO 8859-1實體,這是和htmlrntities()的區別

預定義的字元是:

& (和號) 成為 &
” (雙引號) 成為 "
‘ (單引號) 成為 '
< (小於) 成為 &lt;
> (大於) 成為 &gt;

2.2   htmlspecialchars(string,quotestyle,character-set)

2.3   參數htmlentities()

2.4   提示:無法被識別的字元集將被忽略,並由 ISO-8859-1 代替。

$str = "John & 'Adams'";
echo htmlentities($str);
//在瀏覽器中輸出:John & 'Adams'
//查看原始碼:John &amp; 'Adams'

二、html_entity_decode()和htmlspecialchars_decode()

html_entity_decode(string,quotestyle,character-set) 函數把 HTML 實體轉換為字元,是htmlentities()的反函數。

htmlspecialchars_decode(string,quotestyle)函數把預定義的 HTML 實體轉換為字元,是htmlspecialchars()的反函數。

$str = "John &amp; &#039;Adams&#039;";
echo html_entity_decode($str);
//瀏覽器輸出:John & 'Adams'
//原始碼:John & &#039;Adams&#039;

三、addslashes()和addcslashes()

1、addslashes(string):在指定的預定義字元前添加反斜線。string是需要檢查的字串。該函數數可用於為儲存在資料庫中的字串以及資料庫查詢語句準備合適的字串。

預定義字元是:單引號(’)、雙引號(”)、反斜扛()和NULL

ps:預設情況下,PHP 指令 magic_quotes_gpc 為 on,對所有的 GET、POST 和 COOKIE 資料自動運行 addslashes()。不要對已經被 magic_quotes_gpc 轉義過的字串使用 addslashes(),因為這樣會導致雙層轉義。遇到這種情況時可以使用函數 get_magic_quotes_gpc() 進行檢測。

$str = "Who's John Adams?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";

輸出:

Who's John Adams? This is not safe in a database query.
Who's John Adams? This is safe in a database query.

2、addcslashes(string,characters) 函數在指定的字元前添加反斜線。stirng必須,第二個可選。規定受 addcslashes() 影響的字元或字元範圍。

ps:在對 0,r,n 和 t 應用 addcslashes() 時要小心。在 PHP 中,,r,n 和 t 是預定義的逸出序列。此函數可以對任何字元,包括預定義字元進行反斜扛添加,這是和addslashes的區別

//向特定字元添加反斜線
$str = "Hello, my name is John Adams.";
echo $str;
echo addcslashes($str,'m');
echo addcslashes($str,'J');

輸出:

Hello, my name is John Adams.
Hello, my name is John Adams.
Hello, my name is John Adams.

//向字串中的一個範圍內的字元添加反斜線
$str = "Hello, my name is John Adams.";
echo $str;
echo addslashes($str);   //使用addslashes
echo addcslashes($str,'A..Z');
echo addcslashes($str,'a..z');
echo addcslashes($str,'a..h');

輸出:

Hello, my name is John Adams.

Hello, my name is John Adams.

Hello, my name is John Adams.

Hello, my name is John Adams.

Hello, my name is John Adams.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.