addslashes

(PHP 3, PHP 4, PHP 5)

addslashes -- 使用反斜線引用字串

描述

string addslashes ( string str )

返回字串,該字串為了資料庫查詢語句等的需要在某些字元前加上了反斜線。這些字元是單引號(')、雙引號(")、反斜線(\)與 NUL(NULL 字元)。

一個使用 addslashes() 的例子是當你要往資料庫中輸入資料時。例如,將名字 O'reilly 插入到資料庫中,這就需要對其進行轉義。大多資料庫使用 \ 作為轉義符:O\'reilly。這樣可以將資料放入資料庫中,而不會插入額外的 \。當 PHP 指令 magic_quotes_sybase 被設定成 on 時,意味著插入 ' 時將使用 ' 進行轉義。

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

例子 1. addslashes() 示例

<?php
$str 
"Is your name O'reilly?";

// 輸出:Is your name O\'reilly?
echo addslashes($str);
?>

參見 stripslashes()htmlspecialchars()quotemeta()get_magic_quotes_gpc()