Discuz教程网

PHP自定义的Unicode编码转GBK,GBK转Unicode编码函数

[复制链接]
authicon dly 发表于 2012-12-2 22:40:30 | 显示全部楼层 |阅读模式
php中文汉字转Unicode编码,Unicode转中文的函数
  1. /**
  2. * $str 原始中文字符串
  3. * $encoding 原始字符串的编码,默认GBK
  4. * $prefix 编码后的前缀,默认"&#"
  5. * $postfix 编码后的后缀,默认";"
  6. */
  7. function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
  8.     $str = iconv($encoding, 'UCS-2', $str);
  9.     $arrstr = str_split($str, 2);
  10.     $unistr = '';
  11.     for($i = 0, $len = count($arrstr); $i < $len; $i++) {
  12.         $dec = hexdec(bin2hex($arrstr[$i]));
  13.         $unistr .= $prefix . $dec . $postfix;
  14.     }
  15.     return $unistr;
  16. }
复制代码
  1. /**
  2. * $str Unicode编码后的字符串
  3. * $decoding 原始字符串的编码,默认GBK
  4. * $prefix 编码字符串的前缀,默认"&#"
  5. * $postfix 编码字符串的后缀,默认";"
  6. */
  7. function unicode_decode($unistr, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
  8.     $arruni = explode($prefix, $unistr);
  9.     $unistr = '';
  10.     for($i = 1, $len = count($arruni); $i < $len; $i++) {
  11.         if (strlen($postfix) > 0) {
  12.             $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
  13.         }
  14.         $temp = intval($arruni[$i]);
  15.         $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
  16.     }
  17.     return iconv('UCS-2', $encoding, $unistr);
  18. }
复制代码

//GBK字符串测试
$str = '<b>哈哈</b>';
echo $str.'<br />';

$unistr = unicode_encode($str);
echo $unistr.'<br />'; // <b>哈哈</b>

$str2 = unicode_decode($unistr);
echo $str2.'<br />'; //<b>哈哈</b>

//UTF-8字符串测试
$utf8_str = iconv('GBK', 'UTF-8', $str);
echo $utf8_str.'<br />'; // <b>鍝堝搱</b> 注:UTF在GBK下显示的乱码!可切换浏览器的编码测试

$utf8_unistr = unicode_encode($utf8_str, 'UTF-8');
echo $utf8_unistr.'<br />'; // <b>哈哈</b>

$utf8_str2 = unicode_decode($utf8_unistr, 'UTF-8');
echo $utf8_str2.'<br />'; // <b>鍝堝搱</b>

//其它后缀、前缀测试
$prefix_unistr = unicode_encode($str, 'GBK', "\\u", '');
echo $prefix_unistr.'<br />'; // \u60\u98\u62\u21704\u21704\u60\u47\u98\u62

$profix_unistr2 = unicode_decode($prefix_unistr, 'GBK', "\\u", '');
echo $profix_unistr2.'<br />'; //<b>哈哈</b>

上边的是转载的,unicode_decode函数会丢失除unicode编码以外的字符

下边是自己简单修改的unicode_decode函数
  1. /**
  2. * $str Unicode编码后的字符串
  3. * $decoding 原始字符串的编码,默认GBK
  4. * $prefix 编码字符串的前缀,默认"&#"
  5. * $postfix 编码字符串的后缀,默认";"
  6. */
  7. function s_unicode_decode($unistr, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
  8.    
  9.     if(stristr($unistr,$prefix)){
  10.          $arruni = explode($prefix, $unistr);
  11.          $unistr = '';
  12.          for($i = 1, $len = count($arruni); $i < $len; $i++) {
  13.              if (strlen($postfix) > 0) {
  14.                  $arruni2 = array();
  15.                  $arruni2 = explode($postfix, $arruni[$i]);
  16.                  if(count($arruni2) > 1){
  17.                       if(is_numeric($arruni2[0])){
  18.                                  $temp = intval($arruni2[0]);
  19.                                     $unistr .= iconv('UCS-2', $encoding, ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256));
  20.                            if(!empty($arruni2[1])){
  21.                                      $unistr .= $arruni2[1];
  22.                            }
  23.                       }else{
  24.                            $unistr .= $arruni[$i];
  25.                       }
  26.                  }else{
  27.                       $unistr .= $arruni[$i];
  28.                  }
  29.              }else{
  30.                   $temp = intval($arruni[$i]);
  31.                   $unistr .= iconv('UCS-2', $encoding, ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256));
  32.                 }
  33.          }
  34.        }
  35.     return $unistr;
  36. }
复制代码
游客,如果您要查看本帖隐藏内容请回复





上一篇:转载:巴厘岛最初印象
下一篇:Discuz X2.5利用计划任务每日下载Alexa的数据(炎藤)
authicon 亘古つ守侯珊 发表于 2012-12-3 09:00:45 | 显示全部楼层
貌似我们工具中已经存在!!
authicon  楼主| dly 发表于 2012-12-3 11:24:58 | 显示全部楼层
亘古つ守侯珊 发表于 2012-12-3 09:00
貌似我们工具中已经存在!!


貌似我从没用酒鬼的工具,他的是把中文转成unicode(所谓的语言包),我今天用到的是unicode转GBK,呵呵
再貌似还得自己改过一些文件才能用,比如基础数据库都是haoteam_的,呵呵
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-1 23:34

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表