溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

在php 中使用htmlentities導(dǎo)致中文無(wú)法查詢(xún)?nèi)绾谓鉀Q

發(fā)布時(shí)間:2021-06-07 16:20:26 來(lái)源:億速云 閱讀:186 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

在php 中使用htmlentities導(dǎo)致中文無(wú)法查詢(xún)?nèi)绾谓鉀Q?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

在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)成 &
 " (雙引號(hào)) 轉(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ū)別

這兩個(gè)函數(shù)的功能都是轉(zhuǎn)換字符為HTML字符編碼,特別是url和代碼字符串。防止字符標(biāo)記被瀏覽器執(zhí)行。使用中文時(shí)沒(méi)什么區(qū)別,但htmlentities會(huì)格式化中文字符使得中文輸入是亂碼

htmlentities轉(zhuǎn)換所有的html標(biāo)記,htmlspecialchars只格式化& ' " < 和 > 這幾個(gè)特殊符號(hào)

效果:

$str = '<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >測(cè)試頁(yè)面</a>';

echo 'htmlentities指定GB2312編碼:'.htmlentities($str,ENT_COMPAT,"GB2312").'';

echo 'htmlentities未指定編碼:'.htmlentities($str).'';

$str = '<a href="demo.php?m=index&a=index&name=中文" rel="external nofollow" rel="external nofollow" >測(cè)試頁(yè)面</a>';

echo htmlspecialchars($str).'';

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI