程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 繁體中文轉換為簡體中文的PHP函數

繁體中文轉換為簡體中文的PHP函數

編輯:PHP綜合
前幾天我在oso論壇上問有沒有這個源程序,沒人回復。終於下定決心移植一個這樣的函數,感謝網友Keyes提供移植用的Delphi源代碼。其調用方式為$txt=big5togb($txt)。
    (注:源代碼中的include "data_big5.php";這個文件在就是一個數組,在http://caocao.oso.com.cn/data_big5.zip,請編輯下載到oso上,做一個鏈接,因為這個文件我過幾天就要刪除了。)

<?
/***********************************************************************
                       Written by caocao
                       [email protected]
                       http://caocao.oso.com.cn

                       With the help of Keyes
                       [email protected]
                       http://my-wjl.scu.edu.cn/~Keyes
***********************************************************************/
function isbig5($code)
{
  if (strlen($code)>=2)
  {
    $code=strtok($code,"");

    if (ord($code[0]) < 161)
    {
      return (0);
    }
    else
    {
      if (((ord($code[1]) >= 64)&&(ord($code[1]) <= 126))||((ord($code[1]) >= 161)&&(ord($code[1]) <= 254)))
      {
        return (1);
      }
      else
      {
        return (0);
      }
    }
  }
  else
  {
    return (0);
  }
}

function big5offset($code)
{
  if (strlen($code) >= 2)
  {
    $code=strtok($code,"");
    if ((ord($code[1]) >= 64)&&(ord($code[1]) <= 126))
    {
      return ((ord($code[0]) - 161) * 157 + (ord($code[1]) - 64));
    }
    if ((ord($code[1]) >= 161)&&(ord($code[1]) <= 254))
    {
      return ((ord($code[0]) - 161) * 157 + 63 + (ord($code[1]) - 161));
    }
  }
  return (-1);
}

function wordtostring($code)
{
  return (chr(hexdec(substr($code,0,2))).chr(hexdec(substr($code,2,2))));
}

function big5togb($code)
{
  include "data_big5.php";
  $output="";
  $length=strlen($code);
  $code=strtok($code,"");
  $idx=0;
  while ($idx < $length)
  {
    $tmpStr=$code[$idx].$code[$idx+1];

    if (isbig5($tmpStr))
    {
      $offset=big5offset($tmpStr);
      if (($offset >= 0)||($offset <= 14757))
      {
        $output.=wordtostring($big5order[$offset]);
        $idx++;
      }
      else
      {
        $output.= $code[$idx];
      }
    }
    else
    {
      $output.= $code[$idx];
    }
    $idx++;
  }
  return ($output);
}
?>  

【本文版權歸作者與奧索網共同擁有,如需轉載,請注明作者及出處】    
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved