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]的字符

正则例子:

preg_replace("/([x80-xff])/","",$str);

preg_replace("/([u4e00-u9fa5])/","",$str);

二、代码例子

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