PHP addslashes() 函數(shù)
定義和用法
addslashes() 函數(shù)在指定的預(yù)定義字符前添加反斜杠。
這些預(yù)定義字符是:
- 單引號(hào) (')
- 雙引號(hào) (")
- 反斜杠 (\)
- NULL
語法
addslashes(string)
參數(shù) | 描述 |
---|---|
string | 必需。規(guī)定要檢查的字符串。 |
提示和注釋
提示:該函數(shù)可用于為存儲(chǔ)在數(shù)據(jù)庫中的字符串以及數(shù)據(jù)庫查詢語句準(zhǔn)備合適的字符串。
注釋:默認(rèn)情況下,PHP 指令 magic_quotes_gpc 為 on,對(duì)所有的 GET、POST 和 COOKIE 數(shù)據(jù)自動(dòng)運(yùn)行 addslashes()。不要對(duì)已經(jīng)被 magic_quotes_gpc 轉(zhuǎn)義過的字符串使用 addslashes(),因?yàn)檫@樣會(huì)導(dǎo)致雙層轉(zhuǎn)義。遇到這種情況時(shí)可以使用函數(shù) get_magic_quotes_gpc() 進(jìn)行檢測。
例子
在本例中,我們要向字符串中的預(yù)定義字符添加反斜杠:
<?php
$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.