首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 繁簡體中文轉換類別實作

     編輯:PHP 綜合知識

基於對照表的 Big5/GB2312 雙向轉換

本類別採用查表法實現繁簡體中文編碼轉換,需搭配兩個對照表檔案(big5.map、gb.map)。雖然現代專案多改用 mb_convert_encoding() 或 opencc,但此實作展示了編碼轉換的核心邏輯。

類別結構

<?php
class CCharset {
    var $gb_map = "gb.map";
    var $big5_map = "big5.map";
    var $dep_char = 127;  // 判斷雙位元組字元的閾值

    // Big5 → GB2312 單字轉換
    function cbig5_gb($str, $fd) {
        $c = ord(substr($str, 0, 1));
        $x = ord(substr($str, 1, 1));
        $address = (($c - 160) * 510) + ($x - 1) * 2;
        fseek($fd, $address);
        return fgetc($fd) . fgetc($fd);
    }

    // GB2312 → Big5 單字轉換
    function cgb_big5($str, $fd) {
        $c = ord(substr($str, 0, 1));
        $x = ord(substr($str, 1, 1));
        $address = (($c - 160) * 510) + ($x - 1) * 2;
        fseek($fd, $address);
        return fgetc($fd) . fgetc($fd);
    }

    // 字串級 Big5 → GB
    function Big5_Gb($str) {
        $fd = fopen($this->gb_map, "r");
        $str = str_replace("charset=big5", "charset=gb2312", $str);
        $outstr = "";
        for ($i = 0; $i < strlen($str); $i++) {
            $ch = ord(substr($str, $i, 1));
            if ($ch > $this->dep_char) {
                $outstr .= $this->cbig5_gb(substr($str, $i, 2), $fd);
                $i++;
            } else {
                $outstr .= substr($str, $i, 1);
            }
        }
        fclose($fd);
        return $outstr;
    }

    // 字串級 GB → Big5
    function Gb_Big5($str) {
        $fd = fopen($this->big5_map, "r");
        $str = str_replace("charset=gb2312", "charset=big5", $str);
        $outstr = "";
        for ($i = 0; $i < strlen($str); $i++) {
            $ch = ord(substr($str, $i, 1));
            if ($ch > $this->dep_char) {
                $outstr .= $this->cgb_big5(substr($str, $i, 2), $fd);
                $i++;
            } else {
                $outstr .= substr($str, $i, 1);
            }
        }
        fclose($fd);
        return $outstr;
    }
}
?>

使用範例

<?php
include("CCharset.php3");
$obj = new CCharset;

$big5 = "這是一個Big5的文字串";
$gb = $obj->Big5_Gb($big5);
echo $gb;  // 輸出 GB2312 編碼

$big5_again = $obj->Gb_Big5($gb);
echo $big5_again;  // 還原為 Big5
?>

網頁整頁轉換應用

<?php
include("CCharset.php3");
$url = $QUERY_STRING;  // 目標網址
$content = join('', file($url));
$content = str_replace("<head>", "<head><base href=$url>", $content);  // 修正相對路徑

$converter = new CCharset;
$gbContent = $converter->Big5_Gb($content);
echo $gbContent;
?>

現代替代方案

方案優點缺點
mb_convert_encoding()內建函式、支援多種編碼、無需外部檔案繁簡轉換僅限編碼層面,無法處理詞彙差異(如「軟體」vs「软件」)
OpenCC (Open Chinese Convert)開源、支援詞彙級轉換、台灣/香港/大陸用語區分、高效能需安裝擴展或呼叫外部程式
overtrue/pinyin + 字典純 PHP、可客製化詞庫效能較低、需維護詞庫

OpenCC PHP 整合範例

<?php
// 使用 overtrue/opencc (Composer 套件)
require 'vendor/autoload.php';

use OpenCC\OpenCC;

$opencc = new OpenCC('s2twp.json');  // 簡體→繁體(台灣慣用語+異體字)
$traditional = $opencc->convertSimplifiedToTraditional($simplifiedText);

// 或使用 PHP 擴展 php-opencc (效能最佳)
$od = opencc_open('s2twp.json');
$traditional = opencc_convert($od, $simplifiedText);
opencc_close($od);
?>

架構建議

  • 新專案:直接採用 UTF-8 編碼,前端按需透過 JavaScript (如 opencc-js) 轉換顯示
  • 舊系統維護:封裝轉換邏輯為 Service 類別,支援多種後端驅動(OpenCC、mbstring、查表法)
  • 資料庫儲存:統一儲存 UTF-8,避免編碼混雜導致查詢/排序異常
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved