php字符串中轉(zhuǎn)義成特殊字符實例講解
在php的字符串使用時,我們有學(xué)會一些轉(zhuǎn)義字符,相信大家在記憶這些知識點的時候費了不少的功夫。本篇我們?yōu)榇蠹規(guī)淼氖亲址霓D(zhuǎn)義方法,涉及到特殊字符的使用,主要有兩種方法可以實現(xiàn):mysql庫函數(shù)和轉(zhuǎn)義函數(shù)。下面就這兩種方法,在下文中展開詳細(xì)的介紹。
1、轉(zhuǎn)義字符說明雙引號中,所有轉(zhuǎn)義字符都可正常使用。
單引號中,只有單引號轉(zhuǎn)義字符可以使用('),別的都不可使用。
2、利用mysql庫函數(shù)PHP版本在7.0之前:
mysql_escape_string ( string $unescaped_string ) : string
PHP版本在7.0之后:
mysqli_real_escape_string ( mysqli $link , string $escapestr ) : string3、利用轉(zhuǎn)義函數(shù)addslashes()
適合版本PHP4、PHP5、PHP7
addslashes ( string $str ) : string
PHP 5.4 之前 PHP 指令 magic_quotes_gpc 默認(rèn)是 on, 實際上所有的 GET、POST 和 COOKIE 數(shù)據(jù)都用被 addslashes() 了。 不要對已經(jīng)被magic_quotes_gpc 轉(zhuǎn)義過的字符串使用 addslashes(),因為這樣會導(dǎo)致雙層轉(zhuǎn)義。 遇到這種情況時可以使用函數(shù) get_magic_quotes_gpc() 進行檢測。即get_magic_quotes_gpc()返回false時,再使用addslashes()進行特殊字符轉(zhuǎn)義。示例如下
function myaddslashes($data){ if(false == get_magic_quotes_gpc()) { return addslashes($data);//未啟用魔術(shù)引用時,轉(zhuǎn)義特殊字符 } return $data;}
總結(jié):
與PHP字符串轉(zhuǎn)義相關(guān)的配置和函數(shù)如下:
1.magic_quotes_runtime
2.magic_quotes_gpc
3.addslashes()和stripslashes()
4.mysql_escape_string()
5.addcslashes()和stripcslashes()
6.htmlentities() 和html_entity_decode()
7.htmlspecialchars()和htmlspecialchars_decode()
到此這篇關(guān)于php字符串中轉(zhuǎn)義成特殊字符實例講解的文章就介紹到這了,更多相關(guān)php字符串中轉(zhuǎn)義成特殊字符內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
