ç§ã®ææžã¯ã©ã®ãšã³ã³ãŒãã£ã³ã°ã§ãå®å šã«ãã³ã»ã³ã¹ã§ãïŒ
ãããã®ã·ãŒã±ã³ã¹ãïŒäººã®èŠ³ç¹ããïŒåççã«èŠããªãå Žåãããã¯ããã¥ã¡ã³ããç¹å®ã®ç¬éã«èª€ã£ãŠå€æãããå¯èœæ§ãæãé«ãå Žåã§ãã ããšãã°ãÃGÃìÃRà [ÃfÃBÃìÃOÃÃìÃõÃâ ûâãšããããã¹ãã䜿çšããäœãèããã«UTF-8ã§ä¿åããŸãã ããã¹ããšãã£ã¿ãŒã¯ãMac Romanã§ãšã³ã³ãŒããããããã¹ããæ£ããèªã¿ãä»åºŠã¯å¥ã®ãšã³ã³ãŒãã§ä¿åããå¿ èŠãããããšãææ¡ããŸããã æçµçã«ããããã®æåã¯ãã¹ãŠUnicodeã§æå¹ã§ãã ã€ãŸããUnicodeã«ã¯ãÃãGãªã©ã®æ¡é ããããŸãã ãããã£ãŠãUTF-8ã§ä¿åããã ãã§ãã
11000011 10001001 01000111 11000011 10001001 11000011 10101100 11000011 10001001 01010010 11000011 10000101 01011011 11000011 10001001 01100110 11000011 10001001 01000010 11000011 10001001 11000011 10101100 11000011 10001001 01001111 11000011 10000111 11000011 10010101 11000011 10101100 11000011 10010100 11000011 10000111 11000010 10110101 11000011 10000111 11100010 10001001 10100000 11000011 10000111 11000010 10111011 11000011 10000111 11000010 10100010
ãããã£ãŠãããã¹ãÃGÃìÃRà [ÃfÃBÃìÃOÃÃìÃõÃâ ûâã¯ãUTF-8ã®ãããã®ã·ãŒã±ã³ã¹ã§è¡šãããŸãã ãã®ãããã·ãŒã±ã³ã¹ã¯ãå ã®ããã¥ã¡ã³ãã«ãã£ããã®ãšã¯å®å šã«åé¢ãããŠããŸãã ãã®ã·ãŒã±ã³ã¹ãéããšã³ã³ãŒãã£ã³ã°ã«é¢ä¿ãªãããœãŒã¹ããã¹ãã衚瀺ãããããšã¯ãããŸããã 圌ã¯åã«å€±ãããŠããŸãã å ã®Shift-JISãšã³ã³ãŒãã£ã³ã°ãç¥ã£ãŠããŠãããã¹ããMac RomanãšèŠãªããUTF-8ã§ä¿åããå Žåã¯ã埩å ã§ããŸãã ãããããã®ãããªå¥è·¡ã¯ãŸãã§ãã
å€ãã®å Žåãç¹å®ã®ãšã³ã³ãŒãã§ã¯ç¹å®ã®ãããã·ãŒã±ã³ã¹ãæ£ãããããŸããã ASCIIã§å ã®ããã¥ã¡ã³ããéãããšãããšãäžéšã®æåãèªèãããäžéšã®æåãèªèãããªãããšãããããŸãã 䜿çšããããã°ã©ã ã¯ãçŸåšã®ãšã³ã³ãŒãã«é©åããªããã€ããåã«ç Žæ£ããããçå笊ã«çœ®ãæããããšã決å®ã§ããŸãã ãŸãã¯ãUnicodeã®ç¹å¥ãªçœ®ææåïŒ ïŒU + FFFDïŒã äžé©åãªæåãä¿åããåŸã«ããã¥ã¡ã³ããä¿åãããšããããã¯æ°žä¹ ã«å€±ãããŸãã
ãšã³ã³ãŒãã誀ã£ãŠæšæž¬ããããããã1ã€ã«ä¿åãããšãããã¥ã¡ã³ããå°ç¡ãã«ãªããŸãã ããªãã¯ãããä¿®æ£ããããšããããšãã§ããŸããããããã®è©Šã¿ã¯éåžžæåã«çµãããŸããã ãããã·ãããããéæ³ã¯ãéåžžãã¢ã€ãã«ã®éæ³ã®ãŸãŸã§ãããŸãã§æ»ãã 湿åžã®ããã§ãã
ãããŠããšã³ã³ãŒãã£ã³ã°ãå€æŽããæ¹æ³ã¯ïŒ
ããã¯æ¬åœã«ç°¡åã§ãïŒ ç¹å®ã®ããã¹ãïŒãããã·ãŒã±ã³ã¹ïŒã®ãšã³ã³ãŒãã£ã³ã°ãç¥ããããã埩å·åã«é©çšããå¿ èŠããããŸãã ãããè¡ãããå¿ èŠããããã¹ãŠã§ãã ãŠãŒã¶ãŒããããã¹ããåãåãããã°ã©ã ãäœæããŠããå Žåãã©ã®ãšã³ã³ãŒãã§ãããè¡ããã決å®ããŸãã ããã¹ããã£ãŒã«ãã¯ãã©ã®ãšã³ã³ãŒãã§ããŒã¿ãåãåãããç¥ã£ãŠããå¿ èŠããããŸãã ãŠãŒã¶ãŒãããã°ã©ã ã«ããŒãã§ãããã¡ã€ã«ã®ã¿ã€ãã«ã€ããŠã¯ããšã³ã³ãŒããå®çŸ©ããå¿ èŠããããŸãã ãŸãã¯ããŠãŒã¶ãŒã«è³ªåããæ¹æ³ãå¿ èŠã§ãã æ å ±ã¯ããã¡ã€ã«åœ¢åŒãŸãã¯ãŠãŒã¶ãŒãæäŸã§ããŸãïŒã»ãšãã©ã®äººã¯ããã¡ããèšäºãèªããŸã§ç¥ããŸããïŒã
ãããšã³ã³ãŒãã£ã³ã°ããå¥ã®ãšã³ã³ãŒãã£ã³ã°ã«ããã¹ãã転éããå¿ èŠãããå Žåã¯ãç¹å¥ãªããŒã«ã䜿çšããŠãã ããã å€æã¯ã2ã€ã®ã³ãŒãããŒãžãæ¯èŒãããšã³ã³ãŒãã£ã³ã°Aã®æå152ããšã³ã³ãŒãã£ã³ã°Bã®æå4122ãšäžèŽããããšã決å®ãããã®åŸããããå€æŽãããšããé¢åãªäœæ¥ã§ãã è»èŒªãåçºæããå¿ èŠã¯ãããŸããããã¹ãŠã®äžè¬çãªããã°ã©ãã³ã°èšèªã«ã¯ãããã¹ãããšã³ã³ãŒããããšã³ã³ãŒãã«å€æããããã®ãããããã³ã³ãŒãããŒãžããæœè±¡åãããããŒã«ããããŸãã
ã¢ããªã±ãŒã·ã§ã³ãGB18030ã®ãã¡ã€ã«ãåãå ¥ããå¿ èŠãããããå éšçã«ã¯UTF-32ã§äœæ¥ããŠãããšããŸãã iconvããŒã«ã¯ãiconvïŒ 'GB18030'ã 'UTF-32'ã$æååïŒã®1è¡ã§å€æãå®è¡ã§ããŸãã ãããè¡šçŸãå€æŽãããŠããæåã¯å€æŽãããŸããã
æåGB18030ãšã³ã³ãŒãUTF-32ãšã³ã³ãŒã
瞧10111111 01101100 00000000 00000000 01111110 00100111
以äžã§ãã 圌ã®äººéã®ç解ã«ãããæååã®å 容ã¯å€æŽãããŠããŸããããçŸåšã¯UTF-32ã®æ£ããæååã§ãã UTF-32ã§åŒãç¶ãäœæ¥ããå Žåãèªããªãæåã«åé¡ã¯ãããŸããã ãã ããåè¿°ããããã«ããã¹ãŠã®ãšã³ã³ãŒãããã¹ãŠã®æåã衚瀺ã§ããããã§ã¯ãããŸããã ãšãŒãããèšèªã®ãšã³ã³ãŒãã§ã¯ãæåencodeããšã³ã³ãŒãããããšã¯ã§ããŸããã ãããŠãæãããããšãèµ·ãããããããŸããã
Unicodeã®ãã¹ãŠ
ã ããããã21äžçŽã«ã¯Unicodeã䜿çšããªããšããèšãèš³ã¯ãããŸããã ç¹å®ã®èšèªã§ã¯ããšãŒãããèšèªçšã®äžéšã®ç¹æ®ãªãšã³ã³ãŒãã¯Unicodeãããçç£æ§ãé«ãå ŽåããããŸãã ãããããã©ãã€ãã®ç¹å¥ãªããã¹ããæ±ãå¿ èŠã¯ãããŸãããïŒããã¯éåžžã«å€ãïŒãå¿é ããå¿ èŠã¯ãããŸããã äºææ§ã®ãªããšã³ã³ãŒãã£ã³ã°ã«èµ·å ããåé¡ã¯ãã®ã¬ãã€ãã®æ倱ãããã¯ããã«æ·±å»ã§ãã ãããŠããã®è°è«ã¯ãããŒã¿ã¹ãã¬ãŒãžãšãã£ãã«å¹ ã®æé·ãšã³ã¹ãåæžã«ãã£ãŠã®ã¿éèŠã«ãªããŸãã
ã·ã¹ãã ãä»ã®ãšã³ã³ãŒãã£ã³ã°ã䜿çšããå¿ èŠãããå ŽåããŸãããã¹ããUnicodeã«å€æããã©ããã«è¡šç€ºããå¿ èŠãããå Žåã¯ããã転éããŸãã ããã§ãªãå Žåã¯ãããŒã¿ã«ã¢ã¯ã»ã¹ããåã±ãŒã¹ãæ éã«ç£èŠããå¯èœã§ããã°æ å ±ã倱ãããšãªãå¿ èŠãªå€æãå®è¡ããå¿ èŠããããŸãã
幞ããªå¶ç¶
ããŒã¿ããŒã¹ã«æ¥ç¶ãããWebãµã€ãããããŸããã ç§ã®ã¢ããªã±ãŒã·ã§ã³ã¯UTF-8ã®ãããªãã¹ãŠãåŠçãããã®ããã«ããŒã¿ããŒã¹ã«ä¿åããŸãããããã¹ãŠãã¹ãŒããŒã§ããããããŒã¿ããŒã¹ã®ç®¡çããã«ã«è¡ã£ããšããäœãç解ã§ããŸããã§ããã
-å¿åã®bydloder
ãšã³ã³ãŒããæ£ããåŠçãããªãå ŽåããããŸããããã¹ãŠãæ£åžžã«æ©èœããŸãã ããŒã¿ããŒã¹ã®ãšã³ã³ãŒããlatin-1ã«èšå®ãããã¢ããªã±ãŒã·ã§ã³ãUTF-8ïŒãŸãã¯ãã®ä»ïŒã§åäœããããšããããããŸãã äžè¬ã«ã1ãš0ã®ä»»æã®çµã¿åããã¯ãã·ã³ã°ã«ãã€ãlatin-1ã§æå¹ã§ãã ããŒã¿ããŒã¹ãã¢ããªã±ãŒã·ã§ã³ãããã©ãŒã 11100111 10111000 10100111ã®ããŒã¿ãã¢ããªã±ãŒã·ã§ã³ããåä¿¡ããå Žåãã¢ããªã±ãŒã·ã§ã³ã瞧ãæå³ãããšèããŠãåãã§ããããä¿åããŸãã ã©ãããŠïŒ åŸã§ãããŒã¿ããŒã¹ã¯åãããããã¢ããªã±ãŒã·ã§ã³ã«è¿ããŸããããã¯ãæå³ããUTF-8ã·ã³ãã«receivedãåãåã£ããããæºè¶³ããŠããŸãã ããããdb管çã€ã³ã¿ãŒãã§ã€ã¹ã¯latin-1ã䜿çšãããŠããããšãèªèããŠãããçµæã¯æ¬¡ã®ãšããã§ããç解ããããšã¯äžå¯èœã§ãã
æãè ã¯åã«å®ããã«åœãã£ãããæã¯åœŒã®å³æ¹ã§ã¯ãªãã£ãã ããŒã¿ããŒã¹å ã®ããã¹ãã«å¯Ÿããæäœã¯ãã¹ãŠæ©èœããŸãããããŒã¿ããŒã¹ãããã¹ããæ£ãã解éããªããããæå³ãããšããã«å®è¡ãããªãå ŽåããããŸãã ææªã®å ŽåãããŒã¿ããŒã¹ã¯èª€ã£ãŠããã¹ãå šäœãç Žå£ãã誀ã£ããšã³ã³ãŒãã®ããã«ã€ã³ã¹ããŒã«åŸ2幎ã§ä»»æã®æäœãå®è¡ããŸãïŒãã¡ããããã¯ã¢ããã¯ãããŸããïŒã
UTF-8ããã³ASCII
UTF-8ã®å€©æã¯ASCIIãšã®ãã€ããªäºææ§ã§ãããããã¯ãã¹ãŠã®ãšã³ã³ãŒãã£ã³ã°ã®äºå®äžã®åºç€ã§ãã ãã¹ãŠã®ASCIIæåã¯UTF-8ã§æ倧ãã€ããå æããASCIIãšåããããã䜿çšããŸãã ã€ãŸããASCIIã¯UTF-8ã§1ïŒ1ã«åæ ãããŸãã éASCIIæåã¯ãUTF-8ã§2ãã€ã以äžãå ããŸãã ãœãŒã¹ã³ãŒããšã³ã³ãŒãã£ã³ã°ãšããŠASCIIã䜿çšããã»ãšãã©ã®ããã°ã©ãã³ã°èšèªã§ã¯ãããã¹ãã«çŽæ¥UTF-8ã®ããã¹ããå«ããããšãã§ããŸãã
$ string = "挢å";
UTF-8ã«ä¿åãããšãã·ãŒã±ã³ã¹ã¯æ¬¡ã®ããã«ãªããŸãã
00100100 01110011 01110100 01110010 01101001 01101110 01100111 00100000
00111101 00100000 00100010 11100110 10111100 10100010 11100101 10101101
10010111 00100010 00111011
17ã®12ãã€ãïŒ1ã§å§ãŸããã®ïŒã®ã¿ãUTF-8æåïŒãããã3ãã€ãã®2æåïŒã§ãã ä»ã®æåã¯ASCIIã§ãã ããŒãµãŒã¯æ¬¡ãèªã¿åããŸãã
$ string = "11100110 10111100 10100010 11100101 10101101 10010111";
ããŒãµãŒã¯ãåŒçšç¬Šã®åŸãã®ãã¹ãŠãããã®ãŸãŸå¥ã®åŒçšç¬ŠãŸã§åŠçããå¿ èŠã®ãããããã®ã·ãŒã±ã³ã¹ãšããŠèªèããŸãã ãã®ã·ãŒã±ã³ã¹ãåã«åºåããå Žåã¯ãããã¹ããUTF-8ã§åºåããŸãã ä»ã«äœãããå¿ èŠã¯ãããŸããã ããŒãµãŒã¯ç¹ã«utf-8ããµããŒãããå¿ èŠã¯ãããŸãããæåéãã«è¡ãååŸããã ãã§ãã åçŽãªããŒãµãŒã¯ãå®éã«ã¯UnicodeããµããŒãããªããŠãããã®æ¹æ³ã§UnicodeããµããŒãã§ããŸãã ãã ããå€ãã®ããã°ã©ãã³ã°èšèªã¯ãŠãã³ãŒããæ瀺çã«ãµããŒãããŠããŸãã
ãšã³ã³ãŒãã£ã³ã°ãšPHPã
PHPã¯UnicodeããµããŒãããŠããŸããã 確ãã«ã圌ã¯åœŒãååã«ãµããŒãããŠããŸãã åã®æ®µèœã¯ãUTF-8ãASCIIãšäžäœäºææ§ããããããåé¡ãªãããã°ã©ã ããã¹ãã«UTF-8æåãçŽæ¥å«ããæ¹æ³ã瀺ããŠããŸããããã¯ãã¹ãŠã®PHPã®ããŒãºã§ãã ãã ãããPHPã¯UnicodeããµããŒãããŠããŸããããšãã䞻匵ã¯ãPHPã³ãã¥ããã£ã§å€ãã®åé¡ãåŒãèµ·ãããããçå®ã§ãã
åœãã®çŽæ
utf8_encodeããã³utf8_decodeé¢æ°ã¯ãç§ã®å¯äžã®ã«ã«ã¹ã«ãªããŸããã ãPHPã§Unicodeã䜿çšããã«ã¯ãå ¥åããã¹ãã«utf8_encodeãåŒã³åºããåºåã«utf8_decodeãåŒã³åºãå¿ èŠããããŸããã ããã2ã€ã®é¢æ°ã¯ãäœããã®çš®é¡ã®èªåããã¹ãå€æUTF-8ãçŽæããŸããããã¯ããPHPã¯UnicodeããµããŒãããŠããªãããããšæãããŸãã ãã®èšäºãæãã«èªãã§ããªããªããããªãã¯ãããç¥ã£ãŠãã
- UTF-8ã«åºæã®ãã®ã¯ãããŸãã
- äºåŸãUTF-8ã§ããã¹ãããšã³ã³ãŒãããããšã¯ã§ããŸãã
ãã€ã³ã2ã«ã€ããŠèª¬æããŸãïŒããã¹ãã¯ãã§ã«äœãã§ãšã³ã³ãŒããããŠããŸãã ãœãŒã¹ã³ãŒãã«è¡ã貌ãä»ãããšãæ¢ã«ãšã³ã³ãŒãããããŸãã ããæ£ç¢ºã«ã¯ãããã¹ããšãã£ã¿ãçŸåšäœ¿çšããŠãããšã³ã³ãŒãã£ã³ã°ã ããŒã¿ããŒã¹ããååŸããå Žåããããã¯ãã§ã«ãšã³ã³ãŒããããŠããŸãã ããªãããã¡ã€ã«ããããããèªãã ã...æ¢ã«ç¥ã£ãŠããŸããïŒ
ããã¹ãã¯UTF-8ã§ãšã³ã³ãŒããããŠãããããšã³ã³ãŒããããŠããŸããã ããã§ãªãå Žåã¯ãASCIIãISO-8859-1ãUTF-16ãªã©ã§ãšã³ã³ãŒããããŸãã UTF-8ã§ã¯ãªããããUTF-8æåããå«ãŸããŠãããšæ³å®ãããå Žåãèªç¥çäžååããããŸãã ããã¹ãã«UTF-8ã§ãšã³ã³ãŒããããç®çã®æåããŸã å«ãŸããŠããå Žåã¯ãUTF-8ã«ãªããŸãã
utf8_encodeã¯äžäœäœãããã®ã§ããããïŒ
ãæååISO-8859-1ããšã³ã³ãŒãã£ã³ã°UTF-8ã«å€æããŸãã
ããïŒ èè ã¯ãé¢æ°ãããã¹ããISO-8859-1ããUTF-8ã«å€æãããšèšããããšæã£ãŠããŸããã ããã§ã¯äœã®ããã§ãã ãã®ãããªæãããååã¯ãããŠãªãã®è¯ããšãŒããã人ã«ãã£ãŠåœŒå¥³ã«äžããããã®ã§ãããã utf8_decodeã«ã€ããŠãåãããšãèšããŸãã ãããã®é¢æ°ã¯ãISO-8859-1ããUTF-8ãžã®å€æ以å€ã«ã¯é©çšã§ããŸããã å¥ã®ãšã³ã³ãŒãã£ã³ã°ã®ãã¢ãå¿ èŠãªå Žåã¯ãiconvã䜿çšããŸãã
utf8_encode-ããã¯éæ³ã®æã§ã¯ãããŸããããPHPã¯UnicodeããµããŒãããŠããªãããããã¹ãŠã®åèªãæ¯ãåãå¿ èŠããããŸãã ããã¯è§£æ±ºãããããå€ãã®åé¡ãåŒãèµ·ãããŸã-ãã®ãšãŒããã人ãšç¡ç¥ãªããã°ã©ããŒã®ãããã§ãã
ãã€ãã£ãèŠç¯
ããã§ãèšèªããŠãã³ãŒãããµããŒããããšèšããšãã圌ãã¯äœãæå³ããŸããïŒ èšèªã1æåã1ãã€ããå ãããšä»®å®ãããã©ããã¯éèŠã§ãã ãã®ãããPHPã§ã¯ãéžæããæåã«ã¢ã¯ã»ã¹ããŠãæååãæåé åãšããŠåŠçã§ããŸãã
echo $ string [0];
$æååã«ã·ã³ã°ã«ãã€ããšã³ã³ãŒãã£ã³ã°ãããå Žåãæåã®æåãè¿ãããŸãã ãã ãããæåããã·ã³ã°ã«ãã€ããšã³ã³ãŒãã£ã³ã°ã®ããã€ãããšäžèŽããããã§ãã PHPã¯ãæåã1ã€ãèããã«æåã®ãã€ããè¿ãã ãã§ãã PHPã®æååã¯ããã€ãã·ãŒã±ã³ã¹ã«ããããå€ããå°ãªããããããŸããã ãããã®ãèªã¿åãå¯èœãªæåãã¯ã人ã®ãã£ã¯ã·ã§ã³ã«ãããŸãããPHPã¯ãããã«ã€ããŠæ°ã«ããŸããã
01000100 01101111 01101110 00100111 01110100
D on 't
01100011 01100001 01110010 01100101 00100001
æ°ãã€ããŠïŒ
åãããšããsubstrãstrposãtrimãªã©ã®å€ãã®æšæºé¢æ°ã«ãåœãŠã¯ãŸããŸãã ãµããŒãã¯ããã€ããšæåã®éã®å¯Ÿå¿ãçµäºãããšããã§çµäºããŸãã
11100110 10111100 10100010 11100101 10101101 10010111
挢å
æå®ãããæååã®$ string [0]ã¯ã11100110ã«çããæåã®ãã€ãã®ã¿ãè¿ããŸããã€ãŸãã挢åã®3çªç®ã®ãã€ãã§ãã ã·ãŒã±ã³ã¹11100110ã¯UTF-8ã§ã¯ç¡å¹ã§ãããããæååãæ£ãããããŸããã ãããããªãã«æããå Žåã¯ã11100110ãæå¹ãªã©ã³ãã æåã«ãªãå¥ã®ãšã³ã³ãŒããè©Šãããšãã§ããŸãã 楜ããããšã¯ã§ããŸãããããã«ãµãŒããŒã§ã¯ã§ããŸããã
以äžã§ãã ãPHPã¯UnicodeããµããŒãããŠããŸããããšã¯ãèšèªã®ã»ãšãã©ã®é¢æ°ã1ãã€ãã1æåã«çãããšæ³å®ããŠããããããã«ããã€ãæåã®åãæšãŠãŸãã¯æååã®é·ãã®èª€ã£ãèšç®ã«ã€ãªããããšãæå³ããŸãã ããã¯ãPHPã§Unicodeã䜿çšã§ããªãããšããŸãã¯ããã¹ããutf8_encodeããŸãã¯ä»ã®æããªããšã§å®è¡ããå¿ èŠãããããšãæå³ããŸããã
幞ããªããšã«ããã¹ãŠã®éèŠãªæååé¢æ°ãè¿œå ããç¹å¥ãªæ¡åŒµæ©èœããããŸããããã«ããã€ããšã³ã³ãŒãã£ã³ã°ããµããŒããããŠããŸãã äžèšã®è¡ã®mb_substrïŒ$æååã0ã1ã 'UTF-8'ïŒã¯ã挢åã«å¯Ÿå¿ããã·ãŒã±ã³ã¹11100110 10111100 10100010ãæ£ããè¿ããŸãã é¢æ°ã¯äœãããããèããå¿ èŠããããšããäºå®ã®ããã«ããšã³ã³ãŒãã£ã³ã°ãæž¡ãå¿ èŠããããŸãã ãããã£ãŠããããã®é¢æ°ã¯$ encodingãã©ã¡ãŒã¿ãŒãåãå ¥ããŸãã ãšããã§ããšã³ã³ãŒãã£ã³ã°ã¯ãmb_internal_encodingã䜿çšããŠããã¹ãŠã®mb_é¢æ°ã«å¯ŸããŠã°ããŒãã«ã«èšå®ã§ããŸãã
PHPãšã©ãŒåŠçã®äœ¿çšãšä¹±çš
PHPã§ã®ïŒéïŒUnicodeãµããŒãã®å šäœçãªåé¡ã¯ãã€ã³ã¿ãŒããªã¿ãŒãæ°ã«ããªãããšã§ãã ãã€ãã·ãŒã±ã³ã¹ãhaã ã©ãããæå³ã§ãã æååãã¡ã¢ãªã«ä¿åãã以å€ã¯äœãè¡ãããŸããã PHPã«ã¯ããšã³ã³ãŒããšããæŠå¿µãããããŸããã ãŸããè¡ãæäœããå¿ èŠã¯ãããŸããããããã¯éèŠã§ã¯ãããŸããã äœæ¥ã¯ãã€ãã·ãŒã±ã³ã¹ã§è¡ãããŸããããã¯ã誰ããæåãšããŠèªèããããšãã§ããŸãã PHPã§ã¯ãASCIIãšäºææ§ã®ãããœãŒã¹ã³ãŒããä¿åããã ãã§æžã¿ãŸãã PHPããŒãµãŒã¯ãäœããã¹ãããäŒããç¹å®ã®æåãæ¢ããŠããŸãã 00100100ã«ãããšããå€æ°ã宣èšãããã00111101-ãå²ãåœãŠããã00100010-è¡ã®å é ãŸãã¯æ«å°Ÿãªã©ã ããŒãµãŒã«ãšã£ãŠéèŠã§ã¯ãªããã®ã¯ãã¹ãŠããã€ãã·ãŒã±ã³ã¹ã®ãªãã©ã«ãšããŠèªèãããŸãã ããã¯ãåŒçšç¬Šã§å²ãŸãããã¹ãŠã®ãã®ã«ãé©çšãããŸãã æå³ïŒ
- PHPã§ãœãŒã¹ãASCIIäºææ§ã®ãªããšã³ã³ãŒãã£ã³ã°ã«ä¿åããããšã¯ã§ããŸããã ããšãã°ãUTF-16ã§ã¯ãåŒçšç¬Šã¯00000000 00100010ãšããŠãšã³ã³ãŒããããŸãããã¹ãŠãASCIIãšããŠåŠçããPHPã®å Žåãããã¯åŒçšç¬ŠãåŸã«ç¶ãNULãã€ãã§ãã PHPã¯ããããããã¹ãŠã®æåã§ããã£ããããŠãNULã«ãªãããšãããããŸãã
- PHPãASCIIäºæãšã³ã³ãŒãã£ã³ã°ã«ä¿åã§ããŸãã æåã®128åã®ãšã³ã³ãŒããã€ã³ããASCIIãšäžèŽããå ŽåãPHPã¯ããããé£ã¹ãŸãã PHPã®ãã¹ãŠã®éèŠãªæåã¯ãASCIIã§å®çŸ©ãããæåã®128ãã€ã³ãå ã«ãããŸãã æååãªãã©ã«ã«ãã®å¶éãè¶ ãããã®ãå«ãŸããŠããå ŽåãPHPã¯æ³šæãæããŸããã ãœãŒã¹ã¯ãISO-8859-1ãMac RomanãUTF-8ããŸãã¯ãã®ä»ã®ãšã³ã³ãŒãã£ã³ã°ã§ä¿åã§ããŸãã ã³ãŒãå ã®æååæåã¯ããã¡ã€ã«ãä¿åãããšã³ã³ãŒããåãåããŸãã
- PHPã®å€éšãã¡ã€ã«ã«ã¯ãä»»æã®ãšã³ã³ãŒãã£ã³ã°ã䜿çšã§ããŸãã ããŒãµãŒããã¡ã€ã«ãåŠçããå¿
èŠããªãå Žåãããã¯æºè¶³ããããŸãŸã«ãªããŸãã
$ foo = file_get_contentsïŒ 'bar.txt'ïŒ;
äžèšã¯ãbar.txtã®ãã€ãã$ fooå€æ°ã«åçŽã«å ¥ããŸãã PHPã¯ãã³ã³ãã³ãã®è§£éããšã³ã³ãŒãããŸãã¯ãã®ä»ã®äžæ£è¡çºãè©Šã¿ãŸããã ãã¡ã€ã«ã«ã¯ãã€ããªããŒã¿ãŸãã¯ç»åãå«ãŸããå ŽåããããŸãããPHPã¯æ°ã«ããŸããã - å€éšãšã³ã³ãŒãã£ã³ã°ãšå
éšãšã³ã³ãŒãã£ã³ã°ãäžèŽããå¿
èŠãããå Žåãå®éã«äžèŽããå¿
èŠããããŸãã ããŒã«ãªãŒãŒã·ã§ã³ã¯äžè¬çãªã±ãŒã¹ã§ããã³ãŒãã§ã¯echo localizeïŒ 'Foobar'ïŒã®ãããªãã®ãèšè¿°ããå€éšãã¡ã€ã«ã§ã¯æ¬¡ã®ããã«ããŸãã
msgstr "ããŒããŒ"
msgstr "ããŒããŒ"
äž¡æ¹ã®Foobaræååã¯ãåããããè¡šçŸã§ãªããã°ãªããŸããã ãœãŒã¹ã³ãŒããASCIIã§ãããããŒã«ã©ã€ãºã³ãŒããUTF-16ã§ããå Žåãéãæªãã è¿œå ã®å€æãå®è¡ããå¿ èŠããããŸãã
è³¢æãªèªè ã¯ãããšãã°ããœãŒã¹ãã¡ã€ã«ã®ãªãã©ã«ã«UTF-16ãã€ããé çªã«ASCIIã§ä¿åããããšã¯å¯èœããšå°ãããããããŸããããã¡ãããçãã¯åžžã«æ¬¡ã®ãšããã§ãã
echo "UTF-16";
ãšãã£ã¿ãŒã«åŒ·å¶çã«ãšã³ãŒãããASCIIããã³UTF-16ããUTF-16ã§ä¿åãããšããã¹ãŠãæ©èœããŸãã 以äžã¯ãã€ããªè¡šçŸã§ãã
01100101 01100011 01101000 01101111 00100000 00100010
ãšã³ãŒã
11111110 11111111 00000000 01010101 00000000 01010100
ïŒUTF-16ããŒã«ãŒïŒUT
00000000 01000110 00000000 00101101 00000000 00110001
F-1
00000000 00110110 00100010 00111011
6 ";
æåã®è¡ãšæåŸã®2ãã€ãã¯ASCIIããã®ãã®ã§ãã æ®ãã¯ãæåããšã«2ãã€ãã®UTF-16ã§è¡šãããŸãã 2è¡ç®ã®å é ã®11111110 11111111ã¯ãUTF-16ã®ããã¹ãã®éå§ã瀺ãããŒã«ãŒã§ãïŒæšæºã§å¿ èŠãšããããããPHPã¯ããã«ã€ããŠæ°ã«ããŸããïŒã ãã®ã¹ã¯ãªããã¯ãUTF-16ã§ãšã³ã³ãŒããããæååãUTF-16ãã衚瀺ããŸããããã¯ã2ã€ã®åŒçšç¬Šã®éã®ãã€ãã衚瀺ããã ãã§ãããã¹ããUTF-16ããUTF-16ã§ãšã³ã³ãŒããããããã§ãã äžæ¹ããœãŒã¹ã¯ASCIIãŸãã¯UTF-16ã§å®å šã«æ£ããããã§ã¯ãªãããããšãã£ã¿ãŒãéããŠæ¥œããã§ãã ããã
åèš
PHPã¯UnicodeããµããŒãããŠããŸãããããŒãµãŒã«äœæ¥ãè¡ãããããšãã§ããéçºè ããã®æ©èœãç解ã§ããéãããšã³ã³ãŒãã¯ããªãæ£ç¢ºã§ãã æååãæäœãããšãã®ã¿æ³šæããå¿ èŠããããŸãïŒåå²ãã¹ããŒã¹ã®åé€ãã«ãŠã³ããããã³ãã€ãã§ã¯ãªãæåã®æäœãå¿ èŠãšããä»ã®ãã¹ãŠã®æäœã èªã¿åããšåºåãé€ããŠãè¡ã§äœãè¡ãããªãå Žåãä»ã®èšèªã§ã¯èŠãããªãåé¡ã¯ã»ãšãã©ãããŸããã
ãµããŒããããŠããèšèªã®ãšã³ã³ãŒã
ããã§ã¯ãèšèªããŠãã³ãŒãããµããŒãããããšã¯ã©ãããæå³ã§ããïŒ ããšãã°ãJavascriptã¯UnicodeããµããŒãããŸãã å®éãJavascriptã®æååã¯ãã¹ãŠUTF-8ã§ãšã³ã³ãŒããããŠããŸãã ãããŠãããã¯Javascriptã§åäœããå¯äžã®ãšã³ã³ãŒãã£ã³ã°ã§ãã Javascriptã§UTF-8以å€ã®æååãååŸããªãã§ãã ããã Javascriptã¯UnicodeãåŽæããŸããããã®çšåºŠã¯ãèšèªã®ã³ã¢ã«ç°ãªããšã³ã³ãŒããæäœããããã®ããŒã«ããªãã ãã§ãã Javascriptã¯ã»ãšãã©ã®å Žåãã©ãŠã¶ã§å®è¡ããããããåé¡ã¯ãããŸããããã©ãŠã¶ã¯I / Oã®ãšã³ã³ãŒããšãã³ãŒãã®ç°¡åãªããžãã¯ãå®è¡ã§ããŸãã
ä»ã®èšèªã¯ãšã³ã³ãŒãããµããŒãããã ãã§ãã å éšäœæ¥ã¯ãåäžã®ãšã³ã³ãŒãã£ã³ã°ã§ãå€ãã®å ŽåUTF-16ã§å®è¡ãããŸãã ããããããã¯ãããã¹ãããšã³ã³ãŒãããéã«ããã³ãââãã衚瀺ããå¿ èŠãããããšãæå³ããŸããããããªããšããŠãŒã¶ãŒèªèº«ããããå€æããããšããŸãã ãœãŒã¹ã³ãŒããä¿åãããŠãããšã³ã³ãŒãããã¡ã€ã«ãä¿åãããŠãããšã³ã³ãŒããèªã¿åã察象ã®ãšã³ã³ãŒããåºåã«å¿ èŠãªãšã³ã³ãŒããæå®ããå¿ èŠããããŸãã Unicodeã䜿çšããå¿ èŠãããããšã瀺ãããŠããå Žåãèšèªã¯ãã®å Žã§å€æãããŸãã 圌ãã¯ãPHPãããã¯ã°ã©ãŠã³ãã®ã©ããã§åèªåã¢ãŒãã§è¡ãå¿ èŠãããããšããã¹ãŠè¡ããŸãã PHPã»ã©è¯ããæªãããªãããŸã£ããç°ãªããŸãã 幞ããªããšã«ãæååé¢æ°ã¯ããããæ©èœããããã«ãªããŸãããæååã«ãã«ããã€ãæåãå«ãŸããŠãããã©ãããäœæ¥ã®ããã«éžæããé¢æ°ãããã³PHPã§è¡ãå¿ èŠããããã®ä»ã®ããšãèããå¿ èŠã¯ãããŸããã
ãããªãŠãã³ãŒã
Unicodeã¯éåžžã«å€ãã®ç°ãªãåé¡ã解決ããå€ãã®ç°ãªãã·ããªãªã§æ©èœãããããèéãæã£ãŠä»£éãæ¯æãå¿ èŠããããŸãã ããšãã°ãUnicodeæšæºã«ã¯ãYAKKãžãªã°ãªãã®çµ±äžãªã©ã®åé¡ã®è§£æ±ºã«é¢ããæ å ±ãå«ãŸããŠããŸãã æ¥æ¬ãäžåœãéåœã«å ±éããå€ãã®ãã£ã©ã¯ã¿ãŒã¯ããããã«ç°ãªãæ¹æ³ã§æãããŠããŸãã ãŸãã¯ãæåãå°æåãã倧æåã«ããŸãã¯ãã®éããŸãã¯åŸåŸ©ã«å€æããåé¡ãããã¯ã西ãšãŒãããèšèªã®ãšã³ã³ãŒãã£ã³ã°ã®å Žåã»ã©ç°¡åã§ã¯ãªãããšããããŸãã äžéšã®ãã£ã©ã¯ã¿ãŒã¯ãç°ãªããã€ã³ãã§è¡šãããå ŽåããããŸãã ããšãã°ãæåöã¯ããã©ã°ã©ãU + 00F6ïŒãã©ãã³èªã®å°æåã®ãã£ãšã¬ã·ã¹ãïŒãŸãã¯2ã€ã®ãã©ã°ã©ãU + 006FïŒãå°æåã®ã¬ã¿ãŒOãïŒããã³U + 0308ïŒã代æ¿ç³å°¿ç ãïŒã§è¡šãããšãã§ããŸãã šã§ã UTF-8ã§ã¯ãããã¯2ãã€ããŸãã¯3ãã€ãã§ãã©ã¡ãã®å Žåãéåžžã®æåã§ãã ãããã£ãŠãæšæºã«ã¯æ£èŠåã«ãŒã«ããããŸãã ãããã®ãã©ãŒã ãå¥ã®ãã©ãŒã ã«å€æããæ¹æ³ã ãã以äžã®ããšã¯ãã®èšäºã®ç¯å²ãè¶ ããŠããŸããããããã®ç¹ã«ã€ããŠç¥ãå¿ èŠããããŸãã
åã³ãã€ã¢ã·ãªã«ïŒ
- ããã¹ãã¯åžžã«ãããã®ã·ãŒã±ã³ã¹ã§ãããããŒãã«ã䜿çšããŠèªç¶èšèªã«ç¿»èš³ããå¿ èŠããããŸãã ç¡å¹ãªããŒãã«-ç¡å¹ãªæåã
- ããã¹ããçŽæ¥æäœããããšã¯ã§ããŸãããåžžã«æœè±¡åãããããããæäœããŸãã ãšã©ãŒã¯ãæœè±¡åã®1ã€ã®ãšã©ãŒã«é¢é£ããŠããŸãã
- äºãã«æ å ±ãéä¿¡ããã·ã¹ãã ã¯ãåžžã«æ©èœãããšã³ã³ãŒãã瀺ãå¿ èŠããããŸãã ããšãã°ããµã€ãã¯ãã©ãŠã¶ã«æ å ±ãUTF-8ã«éä¿¡ããŠããããšãäŒããŸãã
- çŸåšãUTF-8ã¯ãã»ãšãã©ãã¹ãŠã®æåããšã³ã³ãŒãã§ããã«ãããããããASCIIãšäžäœäºææ§ããããŸãããã»ãšãã©ã®å Žåæ¯èŒçæå¹ã§ãã ä»ã®ãšã³ã³ãŒãã£ã³ã°ãé©çšãããŸãããUnicodeã®äžéšã®ã¿ããµããŒããããšã³ã³ãŒãã£ã³ã°ã§èŠããæ·±å»ãªçç±ããªããã°ãªããŸããã
- ããã°ã©ã ãšããã°ã©ãã®äž¡æ¹ãããã€ããšæåã®äžèŽã®åé¡ã«å¯ŸåŠããå¿ èŠããããŸãã
åã³ããã¹ããå°ç¡ãã«ãããšãã«æ£åœåãããã®ã¯äœããããŸããã