phpç¼ç _PHPæç¨
程序员文章站
2022-06-01 15:53:12
...
ä¸ã页é¢ç¼ç
ãã1. ä½¿ç¨ æ ç¾è®¾ç½®é¡µé¢ç¼ç
ããè¿ä¸ªæ ç¾çä½ç¨æ¯å£°æ客æ·ç«¯çæµè§å¨ç¨ä»ä¹å符éç¼ç æ¾ç¤ºè¯¥é¡µé¢ï¼xxxå¯ä»¥ä¸ºGB2312ï¼GBKï¼UTF-8ï¼åMySQLä¸åï¼MySQLæ¯ UTF8ï¼ççãå æ¤ï¼å¤§é¨å页é¢å¯ä»¥éç¨è¿ç§æ¹å¼æ¥åè¯æµè§å¨æ¾ç¤ºè¿ä¸ªé¡µé¢çæ¶åéç¨ä»ä¹ç¼ç ï¼è¿æ ·æä¸ä¼é æç¼ç é误è产çä¹±ç ãä½æ¯æçæ¶åæ们ä¼åç°æäºè¿å¥è¿æ¯ä¸è¡ï¼ä¸ç®¡xxxæ¯åªä¸ç§ï¼æµè§å¨éç¨çå§ç»é½æ¯ä¸ç§ç¼ç ï¼è¿ä¸ªæ åµæåé¢ä¼è°å°ã
ãã请注æï¼æ¯å±äºhtmlä¿¡æ¯çï¼ä» ä» æ¯ä¸ä¸ªå£°æï¼å®èµ·ä½ç¨è¡¨ææå¡å¨å·²ç»æHTMLä¿¡æ¯ä¼ å°äºæµè§å¨ã
ãã2. header("content-type:text/html; charset=xxx");
ããè¿ä¸ªå½æ°header()çä½ç¨æ¯ææ¬å·éé¢çä¿¡æ¯åå°httpæ 头ã
ããå¦ææ¬å·éé¢çå 容为æä¸æ说é£æ ·ï¼é£ä½ç¨åæ ç¾åºæ¬ç¸åï¼å¤§å®¶å¯¹ç §ç¬¬ä¸ä¸ªçåç°å符é½å·®ä¸å¤çãä½æ¯ä¸åçæ¯å¦ææè¿æ®µå½æ°ï¼æµè§å¨å°±ä¼æ°¸è¿éç¨ä½ æè¦æ±çxxxç¼ç ï¼ç»å¯¹ä¸ä¼ä¸å¬è¯ï¼å æ¤è¿ä¸ªå½æ°æ¯å¾æç¨çã为ä»ä¹ä¼è¿æ ·å¢ï¼é£å°±å¾è¯´è¯´HTTPSæ 头åHTMLä¿¡æ¯çå·®å«äºï¼
ããhttpsæ 头æ¯æå¡å¨ä»¥HTTPåè®®ä¼ éHTMLä¿¡æ¯å°æµè§å¨åæéåºçå串ã
ããå 为metaæ ç¾æ¯å±äºhtmlä¿¡æ¯çï¼æ以header()åéçå 容å å°è¾¾æµè§å¨ï¼éä¿ç¹å°±æ¯header()çä¼å 级é«äºmetaï¼ä¸ç¥éå¯ä¸å¯ä»¥è¿æ ·è®²ï¼ãå å ¥ä¸ä¸ªphp页é¢æ¢æheader("content-type:text/html; charset=xxx")ï¼åæï¼æµè§å¨å°±åªè®¤åè httpæ 头èä¸è®¤metaäºãå½ç¶è¿ä¸ªå½æ°åªè½å¨php页é¢å 使ç¨ã
ããåæ ·ä¹çæä¸ä¸ªé®é¢ï¼ä¸ºä»ä¹åè å°±ç»å¯¹èµ·ä½ç¨ï¼èåè ææ¶åå°±ä¸è¡å¢ï¼è¿å°±æ¯æ¥ä¸æ¥è¦è°çApacheçåå äºã
ãã3. AddDefaultCharset
ããApache æ ¹ç®å½ç conf æ件夹éï¼ææ´ä¸ªApacheçé ç½®ææ¡£httpd.confã
ããç¨ææ¬ç¼è¾å¨æå¼httpd.confï¼ç¬¬708è¡ï¼ä¸åçæ¬å¯è½ä¸åï¼æAddDefaultCharset xxxï¼xxx为ç¼ç å称ãè¿è¡ä»£ç çææï¼è®¾ç½®æ´ä¸ªæå¡å¨å çç½é¡µæ件httpsæ 头éçå符éä¸ºä½ é»è®¤çxxxå符éãæè¿è¡ï¼å°±ç¸å½äºç»æ¯ä¸ªæ件é½å äºä¸è¡header("content-type:text/html; charset=xxx")ãè¿ä¸å°±æç½ä¸ºä»ä¹ææmeta设置äºæ¯utf-8ï¼å¯æµè§å¨å§ç»éç¨gb2312çåå ã
ããå¦æç½é¡µéæheader("content-type:text/html; charset=xxx")ï¼å°±æé»è®¤çå符éæ¹ä¸ºä½ 设置çå符éï¼æ以è¿ä¸ªå½æ°æ°¸è¿æç¨ãå¦ææAddDefaultCharset xxxåé¢å 个“#”ï¼æ³¨éæè¿å¥ï¼èä¸é¡µé¢éä¸å«header("content-type…")ï¼é£è¿ä¸ªæ¶å就轮å°metaæ ç¾èµ·ä½ç¨äºã
ããæ»ç»ï¼
ããæ¥ä¸ªæåº
ããheader("content-type:text/html; charset=xxx")ãã
ããAddDefaultCharset xxxãã
ãããã
ããå¦æä½ æ¯webç¨åºåï¼ç»ä½ çæ¯ä¸ªé¡µé¢é½å 个header("content-type:text/html; charset=xxx")ï¼ä¿è¯å®å¨ä»»ä½æå¡å¨é½è½æ£ç¡®æ¾ç¤ºï¼å¯ç§»æ¤æ§å¼ºã
ããè³äºé£å¥AddDefaultCharset xxxï¼è¦ä¸è¦æ³¨éå°±ä»è è§ä»äºãåæ£ææ¯æ³¨éæäºï¼ä¸è¿æå页åä¹è¦åheader()ï¼ä¾¿äºæ¾å°æå¡å¨ä¸è½æ£å¸¸æ¾ç¤ºã
ããäºãæ°æ®åºç¼ç
ããPHP ç¨åºå¨æ¥è¯¢æ°æ®åºä¹åï¼é¦å æ§è¡ mysql_query(“SET NAMES xxxx”);å ¶ä¸ xxxx æ¯ä½ ç½é¡µçç¼ç (charset=xxxx)ï¼å¦æç½é¡µä¸ charset=utf8ï¼å xxxx=utf8ï¼å¦æç½é¡µä¸ charset=gb2312ï¼åxxxx=gb2312ï¼å ä¹ææWEBç¨åºï¼é½æä¸æ®µè¿æ¥æ°æ®åºçå ¬å ±ä»£ç ï¼æ¾å¨ä¸ä¸ªæ件éï¼å¨è¿æ件éï¼å å ¥ mysql_query(“set names”)å°±å¯ä»¥äºã
ããSET NAMES æ¾ç¤ºå®¢æ·ç«¯åéç SQL è¯å¥ä¸ä½¿ç¨ä»ä¹å符éãå æ¤ï¼SET NAMES 'utf-8'è¯å¥åè¯æå¡å¨“å°æ¥ä»è¿ä¸ªå®¢æ·ç«¯ä¼ æ¥çä¿¡æ¯éç¨å符éutf-8”ãå®è¿ä¸ºæå¡å¨åéå客æ·ç«¯çç»ææå®äºå符éãï¼ä¾å¦ï¼å¦æä½ ä½¿ç¨ä¸ä¸ªSELECTè¯å¥ï¼å®è¡¨ç¤ºåå¼ä½¿ç¨äºä»ä¹å符éãï¼
ãã1. ä½¿ç¨ æ ç¾è®¾ç½®é¡µé¢ç¼ç
ããè¿ä¸ªæ ç¾çä½ç¨æ¯å£°æ客æ·ç«¯çæµè§å¨ç¨ä»ä¹å符éç¼ç æ¾ç¤ºè¯¥é¡µé¢ï¼xxxå¯ä»¥ä¸ºGB2312ï¼GBKï¼UTF-8ï¼åMySQLä¸åï¼MySQLæ¯ UTF8ï¼ççãå æ¤ï¼å¤§é¨å页é¢å¯ä»¥éç¨è¿ç§æ¹å¼æ¥åè¯æµè§å¨æ¾ç¤ºè¿ä¸ªé¡µé¢çæ¶åéç¨ä»ä¹ç¼ç ï¼è¿æ ·æä¸ä¼é æç¼ç é误è产çä¹±ç ãä½æ¯æçæ¶åæ们ä¼åç°æäºè¿å¥è¿æ¯ä¸è¡ï¼ä¸ç®¡xxxæ¯åªä¸ç§ï¼æµè§å¨éç¨çå§ç»é½æ¯ä¸ç§ç¼ç ï¼è¿ä¸ªæ åµæåé¢ä¼è°å°ã
ãã请注æï¼æ¯å±äºhtmlä¿¡æ¯çï¼ä» ä» æ¯ä¸ä¸ªå£°æï¼å®èµ·ä½ç¨è¡¨ææå¡å¨å·²ç»æHTMLä¿¡æ¯ä¼ å°äºæµè§å¨ã
ãã2. header("content-type:text/html; charset=xxx");
ããè¿ä¸ªå½æ°header()çä½ç¨æ¯ææ¬å·éé¢çä¿¡æ¯åå°httpæ 头ã
ããå¦ææ¬å·éé¢çå 容为æä¸æ说é£æ ·ï¼é£ä½ç¨åæ ç¾åºæ¬ç¸åï¼å¤§å®¶å¯¹ç §ç¬¬ä¸ä¸ªçåç°å符é½å·®ä¸å¤çãä½æ¯ä¸åçæ¯å¦ææè¿æ®µå½æ°ï¼æµè§å¨å°±ä¼æ°¸è¿éç¨ä½ æè¦æ±çxxxç¼ç ï¼ç»å¯¹ä¸ä¼ä¸å¬è¯ï¼å æ¤è¿ä¸ªå½æ°æ¯å¾æç¨çã为ä»ä¹ä¼è¿æ ·å¢ï¼é£å°±å¾è¯´è¯´HTTPSæ 头åHTMLä¿¡æ¯çå·®å«äºï¼
ããhttpsæ 头æ¯æå¡å¨ä»¥HTTPåè®®ä¼ éHTMLä¿¡æ¯å°æµè§å¨åæéåºçå串ã
ããå 为metaæ ç¾æ¯å±äºhtmlä¿¡æ¯çï¼æ以header()åéçå 容å å°è¾¾æµè§å¨ï¼éä¿ç¹å°±æ¯header()çä¼å 级é«äºmetaï¼ä¸ç¥éå¯ä¸å¯ä»¥è¿æ ·è®²ï¼ãå å ¥ä¸ä¸ªphp页é¢æ¢æheader("content-type:text/html; charset=xxx")ï¼åæï¼æµè§å¨å°±åªè®¤åè httpæ 头èä¸è®¤metaäºãå½ç¶è¿ä¸ªå½æ°åªè½å¨php页é¢å 使ç¨ã
ããåæ ·ä¹çæä¸ä¸ªé®é¢ï¼ä¸ºä»ä¹åè å°±ç»å¯¹èµ·ä½ç¨ï¼èåè ææ¶åå°±ä¸è¡å¢ï¼è¿å°±æ¯æ¥ä¸æ¥è¦è°çApacheçåå äºã
ãã3. AddDefaultCharset
ããApache æ ¹ç®å½ç conf æ件夹éï¼ææ´ä¸ªApacheçé ç½®ææ¡£httpd.confã
ããç¨ææ¬ç¼è¾å¨æå¼httpd.confï¼ç¬¬708è¡ï¼ä¸åçæ¬å¯è½ä¸åï¼æAddDefaultCharset xxxï¼xxx为ç¼ç å称ãè¿è¡ä»£ç çææï¼è®¾ç½®æ´ä¸ªæå¡å¨å çç½é¡µæ件httpsæ 头éçå符éä¸ºä½ é»è®¤çxxxå符éãæè¿è¡ï¼å°±ç¸å½äºç»æ¯ä¸ªæ件é½å äºä¸è¡header("content-type:text/html; charset=xxx")ãè¿ä¸å°±æç½ä¸ºä»ä¹ææmeta设置äºæ¯utf-8ï¼å¯æµè§å¨å§ç»éç¨gb2312çåå ã
ããå¦æç½é¡µéæheader("content-type:text/html; charset=xxx")ï¼å°±æé»è®¤çå符éæ¹ä¸ºä½ 设置çå符éï¼æ以è¿ä¸ªå½æ°æ°¸è¿æç¨ãå¦ææAddDefaultCharset xxxåé¢å 个“#”ï¼æ³¨éæè¿å¥ï¼èä¸é¡µé¢éä¸å«header("content-type…")ï¼é£è¿ä¸ªæ¶å就轮å°metaæ ç¾èµ·ä½ç¨äºã
ããæ»ç»ï¼
ããæ¥ä¸ªæåº
ããheader("content-type:text/html; charset=xxx")ãã
ããAddDefaultCharset xxxãã
ãããã
ããå¦æä½ æ¯webç¨åºåï¼ç»ä½ çæ¯ä¸ªé¡µé¢é½å 个header("content-type:text/html; charset=xxx")ï¼ä¿è¯å®å¨ä»»ä½æå¡å¨é½è½æ£ç¡®æ¾ç¤ºï¼å¯ç§»æ¤æ§å¼ºã
ããè³äºé£å¥AddDefaultCharset xxxï¼è¦ä¸è¦æ³¨éå°±ä»è è§ä»äºãåæ£ææ¯æ³¨éæäºï¼ä¸è¿æå页åä¹è¦åheader()ï¼ä¾¿äºæ¾å°æå¡å¨ä¸è½æ£å¸¸æ¾ç¤ºã
ããäºãæ°æ®åºç¼ç
ããPHP ç¨åºå¨æ¥è¯¢æ°æ®åºä¹åï¼é¦å æ§è¡ mysql_query(“SET NAMES xxxx”);å ¶ä¸ xxxx æ¯ä½ ç½é¡µçç¼ç (charset=xxxx)ï¼å¦æç½é¡µä¸ charset=utf8ï¼å xxxx=utf8ï¼å¦æç½é¡µä¸ charset=gb2312ï¼åxxxx=gb2312ï¼å ä¹ææWEBç¨åºï¼é½æä¸æ®µè¿æ¥æ°æ®åºçå ¬å ±ä»£ç ï¼æ¾å¨ä¸ä¸ªæ件éï¼å¨è¿æ件éï¼å å ¥ mysql_query(“set names”)å°±å¯ä»¥äºã
ããSET NAMES æ¾ç¤ºå®¢æ·ç«¯åéç SQL è¯å¥ä¸ä½¿ç¨ä»ä¹å符éãå æ¤ï¼SET NAMES 'utf-8'è¯å¥åè¯æå¡å¨“å°æ¥ä»è¿ä¸ªå®¢æ·ç«¯ä¼ æ¥çä¿¡æ¯éç¨å符éutf-8”ãå®è¿ä¸ºæå¡å¨åéå客æ·ç«¯çç»ææå®äºå符éãï¼ä¾å¦ï¼å¦æä½ ä½¿ç¨ä¸ä¸ªSELECTè¯å¥ï¼å®è¡¨ç¤ºåå¼ä½¿ç¨äºä»ä¹å符éãï¼
ä½è “èè å客”
下一篇: 疑难杂症