关于BIG5-HKSCS的解决方法

yipeiwu_com6年前PHP代码库
非常苦悶地發現,原來一直困擾的HKSCS問題PHP一直也支持。只不過名稱不叫HK-SCS,叫BIG5-HKSCS。
以下是HK增補字符集的解決方案:
HTML頁面設為UTF-8,
寫入數據庫前先:iconv('big5-hkscs','utf8', $string)
需轉為UNICODE就用以下函數
function String2Unicode($data, $language)
{
 $data = nl2br(trim($data));
 $data = str_replace('<br />',chr(13),$data);
 $str = '';
 preg_match_all("/[\x80-\xff]?./",$data,$ar);
 debug($ar); 
 foreach($ar[0] as $v)
 {
  if($v != '' && $v!=chr(13))
  {
   $str .= "&#".utf82unicode(iconv($language,"UTF-8",$v)).";";
  }else {
   $str .=$v;
  }
 }
 return $str;
}
function utf82unicode($c) {
 switch(strlen($c)) {
  case 1:
   return ord($c);
  case 2:
   $n = (ord($c[0]) & 0x3f) << 6;
   $n += ord($c[1]) & 0x3f;
   return $n;
  case 3: 
   $n = (ord($c[0]) & 0x1f) << 12;
   $n += (ord($c[1]) & 0x3f) << 6;
   $n += ord($c[2]) & 0x3f;
   return $n;
  case 4:
   $n = (ord($c[0]) & 0x0f) << 18;
   $n += (ord($c[1]) & 0x3f) << 12; 
   $n += (ord($c[2]) & 0x3f) << 6;
   $n += ord($c[3]) & 0x3f;
   return $n;
 }
}

相关文章

php检索或者复制远程文件的方法

本文实例讲述了php检索或者复制远程文件的方法。分享给大家供大家参考。具体实现方法如下: <?php if(!@copy('http://someserver.com/...

PHP中功能强大却很少使用的函数实例小结

本文实例讲述了PHP中功能强大却很少使用的函数。分享给大家供大家参考,具体如下: call_user_func_array — 让参数以数组的形式调用一个函数 call_user_fun...

PHP运行出现Notice : Use of undefined constant 的完美解决方案分享

Notice: Use of undefined constant ALL_PS - assumed 'ALL_PS' in E:\Server\vhosts\www.lvtao.net...

PHP 事件机制(2)

复制代码 代码如下: <?php class Event extends stdClass{ public $target=null; public $type=null; /**...

PHP 使用openssl 扩展实现公钥加密的方法

如下所示: // 生成私钥 # openssl genrsa -out rsa_private_key.pem 1024 // 生成公钥 # openssl rsa -in rsa_...