gbkºÍutf8±àÂë×Ô¶¯Ê¶±ð·½·¨[php°æ]
±àÂë×Ô¶¯Ê¶±ðµÄ»ù±¾Ë¼ÏëÈçÏ£º
1.¿´¸ø¶¨µÄ×Ö½Ú´®ÊÇ·ñ·ûºÏutf8±àÂë¹æÔò¡£Èç¹û²»·ûºÏÔòΪgbk±àÂë¡£
2.Èç¹û¸ø¶¨µÄ×Ö½Ú´®ÖÐûÓзûºÏutf8Èý×Ö½Ú¹æÔòµÄ£¬ÔòΪgbk±àÂë¡£ÖÐÎÄÔÚutf8ÖÐÕ¼Èý¸ö×Ö½Ú¡£
3.Èç¹û¸ø¶¨µÄ×Ö½Ú´®ÄܶÔÓ¦ÉÏgbk±àÂëÖеÄÖÐÎÄ£¬ÇÒÎÞ·¨¶ÔÓ¦ÉÏutf8±àÂëÖеÄÖÐÎÄ£¬ÔòΪgbk±àÂë¡£
4.ÌØÊâÇé¿ö£¬ÌØÊâ´¦Àí¡£Èç ¡°æœ—l¡± ºÍ ¡°è¨‹Z¡±¡£
×ÜÌå˼ÏëÊÇ£¬ÏÈĬÈÏΪutf8±àÂ룬ÔÙ¸ù¾ÝһЩ·Çutf8±àÂëµÄÇé¿öÖð²½É¸Ñ¡¡£
׼ȷÂʺÍЧÂÊ»¹ÊDz»´íµÄ¡£20Íò¶àÌõquery£¬1ÃëÖӾͿÉÒÔ´¦ÀíÍê¡£
php´úÂëÈçÏ£º
function detect_encoding($str){ $len = strlen($str); $encoding = "utf8"; $is_utf8_chinese = false; for ($i = 0; $i < $len; $i++) { if ( (ord($str[$i]) >> 7) > 0 ) { //·Çascii×Ö·û if (ord($str[$i]) <= 191 ) { $encoding = "gbk0"; break; } else if ( ord($str[$i]) <= 223 ) { //Ç°Á½Î»Îª11 if ( empty($str[$i+1]) or ord($str[$i+1]) >> 6 != 2 ) { //½ô¸úºóÁ½Î»Îª10 $encoding = "gbk1"; break; } else { $i += 1; } } else if ( ord($str[$i]) <= 239 ) { //Ç°ÈýλΪ111 if ( empty($str[$i+1]) or ord($str[$i+1]) >> 6 != 2 or empty($str[$i+2]) or ord($str[$i+2]) >> 6 != 2) { //½ô¸úºóÁ½Î»Îª10 $encoding = "gbk2"; break; } else { $i += 2; $is_utf8_chinese = true; } } else if ( ord($str[$i]) <= 247 ) { //Ç°ËÄλΪ1111 if ( empty($str[$i+1]) or ord($str[$i+1]) >> 6 != 2 or empty($str[$i+2]) or ord($str[$i+2]) >> 6 != 2 or empty($str[$i+3]) or ord($str[$i+3]) >> 6 != 2) { //½ô¸úºóÁ½Î»Îª10 $encoding = "gbk3"; break; } else { $i += 3; } } else if ( ord($str[$i]) <= 251 ) { //Ç°ÎåλΪ11111 if ( empty($str[$i+1]) or ord($str[$i+1]) >> 6 != 2 or empty($str[$i+2]) or ord($str[$i+2]) >> 6 != 2 or empty($str[$i+3]) or ord($str[$i+3]) >> 6 != 2 or empty($str[$i+4]) or ord($str[$i+4]) >> 6 != 2) { //½ô¸úºóÁ½Î»Îª10 $encoding = "gbk4"; break; } else { $i += 4; } } else if ( ord($str[$i]) <= 253 ) { //Ç°ÁùλΪ111111 if ( empty($str[$i+1]) or ord($str[$i+1]) >> 6 != 2 or empty($str[$i+2]) or ord($str[$i+2]) >> 6 != 2 or empty($str[$i+3]) or ord($str[$i+3]) >> 6 != 2 or empty($str[$i+4]) or ord($str[$i+4]) >> 6 != 2 or empty($str[$i+5]) or ord($str[$i+5]) >> 6 != 2 ) { //½ô¸úºóÁ½Î»Îª10 $encoding = "gbk5"; break; } else { $i += 5; } } else { $encoding = "gbk6"; break; } } } if ($is_utf8_chinese == false){ $encoding = "gbk10"; } if ($encoding == "utf8" && preg_match("/^[".chr(0xa1)."-".chr(0xff)."\x20-\x7f]+$/", $str) && !preg_match("/^[\x{4e00}-\x{9fa5}\x20-\x7f]+$/u", $str)) { $encoding = "gbk7"; } //echo $encoding; if ($encoding == "utf8") { //echo "utf8"; return ($str == "朗l" || $str == "訋Z")? $str: mb_convert_encoding($str, "gbk", "utf8"); } else { //echo "gbk"; return $str; } }
¼ÇµÃ´úÂë×îºÃÔÚgbk±àÂëÖÐÔËÐУ¬ÒòΪ´úÂëÖÐÓкº×Ö£¨$str == ¡°æœ—l¡± || $str == ¡°è¨‹Z¡±£©±È½Ï¡£µ±È»Äã¿ÉÒÔÔÚutf8±àÂëÖÐÔËÐУ¬Ö»Òª°Ñºº×Ö´¦ÀíÏ¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ