PHP 字符串编码处理 (附各语言的字符集编码范围)

PHP中GBK和UTF8编码处理 

 

一、编码范围


1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文
x80-xff 中文

2. UTF-8 (Unicode)


u4e00-u9fa5 (中文)
x3130-x318F (韩文)
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)
ps: 韩文是大于[u9fa5]的字符

正则例子:

 

Php代码 复制代码 收藏代码
  1. preg_replace("/([x80-xff])/","",$str);   
  2. preg_replace("/([u4e00-u9fa5])/","",$str);  

 

二、代码例子

 

Php代码 复制代码 收藏代码
  1. //判断内容里有没有中文-GBK (PHP)   
  2. function check_is_chinese($s){   
  3. return preg_match('/[x80-xff]./'$s);   
  4. }   
  5.   
  6.   
  7. //获取字符串长度-GBK (PHP)   
  8. function gb_strlen($str){   
  9. $count = 0;   
  10. for($i=0; $i<strlen($str); $i++){   
  11. $s = substr($str$i, 1);   
  12. if (preg_match("/[x80-xff]/"$s)) ++$i;   
  13. ++$count;   
  14. }   
  15. return $count;   
  16. }   
  17.   
  18. //截取字符串字串-GBK (PHP)   
  19. function gb_substr($str$len){   
  20. $count = 0;   
  21. for($i=0; $i<strlen($str); $i++){   
  22. if($count == $lenbreak;   
  23. if(preg_match("/[x80-xff]/"substr($str$i, 1))) ++$i;   
  24. ++$count;          
  25. }   
  26. return substr($str, 0, $i);   
  27. }   
  28.   
  29.   
  30. //统计字符串长度-UTF8 (PHP)   
  31. function utf8_strlen($str) {   
  32. $count = 0;   
  33. for($i = 0; $i < strlen($str); $i++){   
  34. $value = ord($str[$i]);   
  35. if($value > 127) {   
  36. $count++;   
  37. if($value >= 192 && $value <= 223) $i++;   
  38. elseif($value >= 224 && $value <= 239) $i = $i + 2;   
  39. elseif($value >= 240 && $value <= 247) $i = $i + 3;   
  40. else die('Not a UTF-8 compatible string');   
  41. }   
  42. $count++;   
  43. }   
  44. return $count;   
  45. }   
  46.   
  47.   
  48. //截取字符串-UTF8(PHP)   
  49. function utf8_substr($str,$position,$length){   
  50. $start_position = strlen($str);   
  51. $start_byte = 0;   
  52. $end_position = strlen($str);   
  53. $count = 0;   
  54. for($i = 0; $i < strlen($str); $i++){   
  55. if($count >= $position && $start_position > $i){   
  56. $start_position = $i;   
  57. $start_byte = $count;   
  58. }   
  59. if(($count-$start_byte)>=$length) {   
  60. $end_position = $i;   
  61. break;   
  62. }      
  63. $value = ord($str[$i]);   
  64. if($value > 127){   
  65. $count++;   
  66. if($value >= 192 && $value <= 223) $i++;   
  67. elseif($value >= 224 && $value <= 239) $i = $i + 2;   
  68. elseif($value >= 240 && $value <= 247) $i = $i + 3;   
  69. else die('Not a UTF-8 compatible string');   
  70. }   
  71. $count++;   
  72. }   
  73. return(substr($str,$start_position,$end_position-$start_position));   
  74. }   
  75.   
  76.   
  77. //字符串长度统计-UTF8 [中文3个字节,俄文、韩文占2个字节,字母占1个字节] (Ruby)   
  78. def utf8_string_length(str)   
  79. temp = CGI::unescape(str)   
  80. i = 0;   
  81. j = 0;   
  82. temp.length.times{|t|   
  83. if temp[t] < 127   
  84. i += 1   
  85. elseif temp[t] >= 127 and temp[t] < 224   
  86. j += 1   
  87. if 0 == (j % 2)   
  88. i += 2   
  89. j = 0   
  90. end  
  91. else  
  92. j += 1   
  93. if 0 == (j % 3)   
  94. i +=2   
  95. j = 0   
  96. end  
  97. end  
  98. }   
  99. return i   
  100. }   
  101.   
  102.   
  103. //判断是否是含有韩文-UTF-8 (javascript)   
  104. function checkKoreaChar(str) {   
  105. for(i=0; i<str.length; i++) {   
  106. if(((str.charCodeAt(i) > 0x3130 && str.charCodeAt(i) < 0x318F) || (str.charCodeAt(i) >= 0xAC00 && str.charCodeAt(i) <= 0xD7A3))) {   
  107. return true;   
  108. }   
  109. }   
  110. return false;   
  111. }   
  112.   
  113.   
  114. //判断是否有中文字符-GBK (javascript)   
  115. function check_chinese_char(s){   
  116. return (s.length != s.replace(/[^x00-xff]/g,"**").length);   
  117. }   

 

 

 

编码 字符集编码范围
UTF8 [\x01-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}
UTF16 [\x00-\xd7][\xe0-\xff]|[\xd8-\xdf][\x00-\xff]{2}
Big5 [\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|[\xa1-\xfe])
GBK [\x01-\x7f]|[\x81-\xfe][\x40-\xfe]
GB2312汉字 [\xb0-\xf7][\xa0-\xfe]
GB2312半角标点符号及特殊符号 \xa1[\xa2-\xfe]
GB2312罗马数组及项目序号 \xa2([\xa1-\xaa]|[\xb1-\xbf]|[\xc0-\xdf]|[\xe0-\xe2]|[\xe5-\xee]|[\xf1-\xfc])
GB2312全角标点及全角字母 \xa3[\xa1-\xfe]
GB2312日文平假名 \xa4[\xa1-\xf3]
GB2312日文片假名 \xa5[\xa1-\xf6]
GB18030 [\x00-\x7f]|[\x81-\xfe][\x40-\xfe]|[\x81-\xfe][\x30-\x39][\x81-\xfe][\x30-\x39]
JIS [\x20-\x7e]|[\x21-\x5f]|[\x21-\x7e]{2}
SJIS [\x20-\x7e]|[\xa1-\xdf]|([\x81-\x9f]|[\xe0-\xef])([\x40-\x7e]|[\x80-\xfc])
SJIS全角空格 (?:\x81\x81)
SJIS全角数字 (?:\x82[\x4f-\x58])
SJIS全角大写英文 (?:\x82[\x60-\x79])
SJIS全角小写英文 (?:\x82[\x81-\x9a])
SJIS全角平假名 (?:\x82[\x9f-\xf1])
SJIS全角平假名扩展 (?:\x82[\x9f-\xf1]|\x81[\x4a\x4b\x54\x55])
SJIS全角片假名 (?:\x83[\x40-\x96])
SJIS全角片假名扩展 (?:\x83[\x40-\x96]|\x81[\x45\x5b\x52\x53])
EUC_JP [\x20-\x7e]|\x81[\xa1-\xdf]|[\xa1-\xfe][\xa1-\xfe]|\x8f[\xa1-\xfe]{2}
EUC_JP标点符号及特殊字符 [\xa1-\xa2][\xa0-\xfe]
EUC_JP全角数字 \xa3[\xb0-\xb9]
EUC_JP全角大写英文 \xa3[\xc1-\xda]
EUC_JP全角小写英文 \xa3[\xe1-\xfa]
EUC_JP全角平假名 \xa4[\xa1-\xf3]
EUC_JP全角片假名 \xa3[\xb0-\xb9]|\xa3[\xc1-\xda]|\xa5[\xa1-\xf6][\xa3][\xb0-\xfa]|[\xa1][\xbc-\xbe]|[\xa1][\xdd]
EUC_JP全角汉字 [\xb0-\xcf][\xa0-\xd3]|[\xd0-\xf4][\xa0-\xfe]|[\xB0-\xF3][\xA1-\xFE]|[\xF4][\xA1-\xA6]|[\xA4][\xA1-\xF3]|[\xA5][\xA1-\xF6]|[\xA1][\xBC-\xBE]
EUC_JP全角空格 (?:\xa1\xa1)
EUC半角片假名 (?:\x8e[\xa6-\xdf])
日文半角空格 \x20

 

 

 

 

Php代码 复制代码 收藏代码
  1. public class CnCharsetChecker {   
  2.     /* Support for Chinese(GB2312) characters */  
  3.     // #define isgb2312head(c) (0xa1<=(uchar)(c) && (uchar)(c)<=0xf7)   
  4.     // #define isgb2312tail(c) (0xa1<=(uchar)(c) && (uchar)(c)<=0xfe)   
  5.     public static boolean isGB2312(byte head, byte tail) {   
  6.         int iHead = head & 0xff;   
  7.         int iTail = tail & 0xff;   
  8.         return ((iHead>=0xa1 && iHead<=0xf7 &&   
  9.                  iTail>=0xa1 && iTail<=0xfe) ? true : false);   
  10.     }   
  11.     /* Support for Chinese(GBK) characters */  
  12.     // #define isgbkhead(c) (0x81<=(uchar)(c) && (uchar)(c)<=0xfe)   
  13.     // #define isgbktail(c) ((0x40<=(uchar)(c) && (uchar)(c)<=0x7e)   
  14.     //                      || (0x80<=(uchar)(c) && (uchar)(c)<=0xfe))   
  15.     public static boolean isGBK(byte head, byte tail) {   
  16.         int iHead = head & 0xff;   
  17.         int iTail = tail & 0xff;   
  18.         return ((iHead>=0x81 && iHead<=0xfe &&   
  19.                  (iTail>=0x40 && iTail<=0x7e ||   
  20.                   iTail>=0x80 && iTail<=0xfe)) ? true : false);   
  21.     }   
  22.     /* Support for Chinese(BIG5) characters */  
  23.     // #define isbig5head(c) (0xa1<=(uchar)(c) && (uchar)(c)<=0xf9)   
  24.     // #define isbig5tail(c) ((0x40<=(uchar)(c) && (uchar)(c)<=0x7e)   
  25.     //                       || (0xa1<=(uchar)(c) && (uchar)(c)<=0xfe))   
  26.     public static boolean isBIG5(byte head, byte tail) {   
  27.         int iHead = head & 0xff;   
  28.         int iTail = tail & 0xff;   
  29.         return ((iHead>=0xa1 && iHead<=0xf9 &&   
  30.                  (iTail>=0x40 && iTail<=0x7e ||   
  31.                   iTail>=0xa1 && iTail<=0xfe)) ? true : false);   
  32.     }   
  33.   
  34.     public static void main(String[] args) {   
  35.         String sGB = "爱";   
  36.         String sGBK = "愛";   
  37.         String sBIG5 = "稲";   
  38.         byte[] sChars = null;   
  39.         sChars = sGB.getBytes();   
  40.         System.out.println(sGB + " is " +   
  41.             CnCharsetChecker.isGB2312(sChars[0],sChars[1]) + " for GB2312;" +   
  42.             CnCharsetChecker.isGBK(sChars[0],sChars[1]) + " for GBK," +   
  43.             CnCharsetChecker.isBIG5(sChars[0],sChars[1]) + " for BIG5");   
  44.         sChars = sGBK.getBytes();   
  45.         System.out.println(sGBK + " is " +   
  46.             CnCharsetChecker.isGB2312(sChars[0],sChars[1]) + " for GB2312;" +   
  47.             CnCharsetChecker.isGBK(sChars[0],sChars[1]) + " for GBK," +   
  48.             CnCharsetChecker.isBIG5(sChars[0],sChars[1]) + " for BIG5");   
  49.         sChars = sBIG5.getBytes();   
  50.         System.out.println(sBIG5 + " is " +   
  51.             CnCharsetChecker.isGB2312(sChars[0],sChars[1]) + " for GB2312;" +   
  52.             CnCharsetChecker.isGBK(sChars[0],sChars[1]) + " for GBK," +   
  53.             CnCharsetChecker.isBIG5(sChars[0],sChars[1]) + " for BIG5");   
  54.     }   
  55. }  

 

时间:2011-12-06 | 分类:技术文摘 | 标签: PHP 
评论列表
暂无评论
发表评论
昵称
邮箱
内容