在php中htmlspecialchars, 將特殊字元轉(zhuǎn)成 HTML 格式,而htmlentities,將所有的字元都轉(zhuǎn)成 HTML 字串 了,下面我來分別簡單的介紹。
htmlentities用法
$str = "John 'Adams'"; echo htmlentities($str, ENT_COMPAT); echo " "; echo htmlentities($str, ENT_QUOTES); echo " "; echo htmlentities($str, ENT_NOQUOTES); ?> John 'Adams' John 'Adams' John 'Adams'
瀏覽器輸出:
htmlspecialchars用法
(和) 轉(zhuǎn)成
" (雙引號) 轉(zhuǎn)成 "
(小于) 轉(zhuǎn)成
> (大于) 轉(zhuǎn)成 >
例
$str = "John 'Adams'"; echo htmlspecialchars($str, ENT_COMPAT); echo " "; echo htmlspecialchars($str, ENT_QUOTES); echo " "; echo htmlspecialchars($str, ENT_NOQUOTES); ?>
他們的區(qū)別
這兩個函數(shù)的功能都是轉(zhuǎn)換字符為HTML字符編碼,特別是url和代碼字符串。防止字符標(biāo)記被瀏覽器執(zhí)行。使用中文時沒什么區(qū)別,但htmlentities會格式化中文字符使得中文輸入是亂碼
htmlentities轉(zhuǎn)換所有的html標(biāo)記,htmlspecialchars只格式化 ' " 和 > 這幾個特殊符號
效果:
$str = 'a href="demo.php?m=indexa=indexname=中文" rel="external nofollow" rel="external nofollow" >測試頁面/a>';
echo 'htmlentities指定GB2312編碼:'.htmlentities($str,ENT_COMPAT,"GB2312").'';
echo 'htmlentities未指定編碼:'.htmlentities($str).'';
$str = 'a href="demo.php?m=indexa=indexname=中文" rel="external nofollow" rel="external nofollow" >測試頁面/a>';
echo htmlspecialchars($str).'';
總結(jié)
以上所述是小編給大家介紹的php 中htmlentities導(dǎo)致中文無法查詢問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
標(biāo)簽:太原 衡陽 鄂州 廊坊 辛集 白銀 綏化 崇左
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php 中htmlentities導(dǎo)致中文無法查詢問題》,本文關(guān)鍵詞 php,中,htmlentities,導(dǎo)致,中文,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。