示例#1
0
 /**   
 * 编码互换   
 *   
 * @param string $source   
 * @param string $source_lang 输入编码 'utf-8' or 'gb2312' or 'big5'   
 * @param string $target_lang 输出编码 'utf-8' or 'gb2312' or 'big5'   
 * @return string   
 */
 public static function convert($source, $source_lang, $target_lang = 'utf-8')
 {
     if ($source_lang != '') {
         $source_lang = str_replace(array('gbk', 'utf8', 'big-5'), array('gb2312', 'utf-8', 'big5'), strtolower($source_lang));
     }
     if ($target_lang != '') {
         $target_lang = str_replace(array('gbk', 'utf8', 'big-5'), array('gb2312', 'utf-8', 'big5'), strtolower($target_lang));
     }
     if ($source_lang == $target_lang || $source == '') {
         return $source;
     }
     $index = $source_lang . "_" . $target_lang;
     if (USEEXISTS && !in_array($index, array('gb2312_big5', 'big5_gb2312'))) {
         //繁简互换并不是交换字符集编码
         if (function_exists('iconv')) {
             return iconv($source_lang, $target_lang, $source);
         }
         if (function_exists('mb_convert_encoding')) {
             return mb_convert_encoding($source, $target_lang, $source_lang);
         }
     }
     $table = self::loadtable($index);
     if (!$table) {
         return $source;
     }
     self::$string = $source;
     self::$source_lang = $source_lang;
     self::$target_lang = $target_lang;
     if ($source_lang == 'gb2312' || $source_lang == 'big5') {
         if ($target_lang == 'utf-8') {
             self::$table = $table;
             return self::CHS2UTF8();
         }
         if ($target_lang == 'gb2312') {
             self::$table = array_flip($table);
         } else {
             self::$table = $table;
         }
         return self::BIG2GB();
     } elseif (self::$source_lang == 'utf-8') {
         self::$table = array_flip($table);
         return self::UTF82CHS();
     }
     return NULL;
 }
示例#2
0
 protected static function BIG2GB()
 {
     $ret = '';
     while (self::$string) {
         if (ord(self::$string[0]) > 0x80) {
             $index = hexdec(bin2hex(self::$string[0] . self::$string[1]));
             $value = self::$table[$index];
             $ret .= self::hex2bin(dechex($value));
             self::$string = substr(self::$string, 2);
         } else {
             $ret .= self::$string[0];
             self::$string = substr(self::$string, 1);
         }
     }
     return $ret;
 }