当前位置:首页 > PHP教程 > php函数 > 列表

PHP中GBK和UTF8编码处理(中文,韩文)

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-16 09:36:18 浏览: 评论:0 

一、编码范围

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.  
  42. //截取字符串-utf8(php) 
  43. function utf8_substr($str,$position,$length){ 
  44.     $start_position = strlen($str); 
  45.     $start_byte = 0; 
  46.     $end_position = strlen($str); 
  47.     $count = 0; 
  48.     for($i = 0; $i < strlen($str); $i++){ 
  49.         if($count >= $position && $start_position > $i){ 
  50.             $start_position = $i; 
  51.             $start_byte = $count; 
  52.         } 
  53.         if(($count-$start_byte)>=$length) { 
  54.             $end_position = $i; 
  55.             break; 
  56.         }    
  57.         $value = ord($str[$i]); 
  58.         if($value > 127){ 
  59.             $count++; 
  60.             if($value >= 192 && $value <= 223) $i++; 
  61.             elseif($value >= 224 && $value <= 239) $i = $i + 2; 
  62.             elseif($value >= 240 && $value <= 247) $i = $i + 3; 
  63.             else die('not a utf-8 compatible string'); 
  64.         } 
  65.         $count++; 
  66.     } 
  67.     return(substr($str,$start_position,$end_position-$start_position)); 
  68. } 
  69.  
  70. //字符串长度统计-utf8 [中文3个字节,俄文、韩文占2个字节,字母占1个字节] (ruby) 
  71. def utf8_string_length(str) 
  72.     temp = cgi::unescape(str) 
  73.     i = 0; 
  74.     j = 0; 
  75.     temp.length.times{|t| 
  76.         if temp[t] < 127 
  77.             i += 1 
  78.         elseif temp[t] >= 127 and temp[t] < 224 
  79.             j += 1 
  80.             if 0 == (j % 2) 
  81.                 i += 2 
  82.                 j = 0 
  83.             end 
  84.         else 
  85.             j += 1 
  86.             if 0 == (j % 3) 
  87.                 i +=2 
  88.                 j = 0 
  89.             end 
  90.         end 
  91.     } 
  92.     return i 
  93. } 
  94.  
  95. //判断是否是含有韩文-utf-8 (网页特效) 
  96. function checkkoreachar(str) { 
  97.     for(i=0; i<str.length; i++) { 
  98.         if(((str.charcodeat(i) > 0x3130 && str.charcodeat(i) < 0x318f) || (str.charcodeat(i) >= 0xac00 && str.charcodeat(i) <= 0xd7a3))) { 
  99.             return true; 
  100.         } 
  101.     } 
  102.     return false; 
  103. }//开源代码phpfensi.com 
  104.  
  105. //判断是否有中文字符-gbk (javascript) 
  106. function check_chinese_char(s){ 
  107.     return (s.length != s.replace(/[^x00-xff]/g,"**").length); 
  108. } 

Tags: PHP编码 UTF8编码处理

分享到: