php轉(zhuǎn)義單引號的方法:使用【addslashes()】函數(shù)在指定的預(yù)定義字符前添加反斜杠,語法為【addslashes(string)】,string是必需,規(guī)定要檢查的字符串。
本教程操作環(huán)境:windows7系統(tǒng)、PHP5.6版,DELL G3電腦。
php轉(zhuǎn)義單引號的方法:
PHP addslashes() 函數(shù)
定義和用法
addslashes()
函數(shù)在指定的預(yù)定義字符前添加反斜杠。
這些預(yù)定義字符是:
-
單引號 (')
-
雙引號 (")
-
反斜杠 ()
-
NULL
語法
addslashes(string)
參數(shù) 描述
string 必需。規(guī)定要檢查的字符串。
提示和注釋
提示:該函數(shù)可用于為存儲在數(shù)據(jù)庫中的字符串以及數(shù)據(jù)庫查詢語句準(zhǔn)備合適的字符串。
注釋:
默認(rèn)情況下,PHP 指令 magic_quotes_gpc
為 on,對所有的 GET、POST 和 COOKIE 數(shù)據(jù)自動運行 addslashes()。
不要對已經(jīng)被 magic_quotes_gpc
轉(zhuǎn)義過的字符串使用 addslashes(),因為這樣會導(dǎo)致雙層轉(zhuǎn)義。遇到這種情況時可以使用函數(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.
一般按以下形式使用
if(!(get_magic_quotes_gpc())) { $_GET = addslashes($_GET); $_POST = addslashes($_POST); $_COOKIE = addslashes($_COOKIE); }
相關(guān)視頻推薦:PHP視頻教程