qswhU 類別專注將各種 Unicode 表示形式轉換為 GBK:
〹あ%E3%81%82%u3042內建四種過濾模式,並支援自訂回呼擴充。
<?php
class qswhU {
var $qswhData;
function qswhU($filename = "qswhU.php") {
$this->qswhData = file($filename); // 載入對照表
}
function decode($str, $pattern = 0) {
$patterns = [
'/&#(\w+);/iU', // 0: 十進位實體
'/((%\w\w)+)/i', // 1: URL 編碼
'/%u(\w{4,5})/iU' // 2: %uXXXX
];
if (is_int($pattern)) $pattern = $patterns[$pattern];
return preg_replace_callback($pattern, [$this, 'u2gb'], $str);
}
function u2gb($matches) {
// 解析 Unicode 碼位 -> 查表 -> 組合 GBK 雙位元組
// 詳細邏輯見原始碼 (位運算、查表映射)
}
}
?>
<?php
require 'qswhU.php';
$conv = new qswhU();
// 轉換 UTF-8 字串
$utf8 = "測試";
echo $conv->decode($utf8, 3); // 模式 3 為 UTF-8 (原碼註解暗示)
// 轉換 HTML 實體
$html = "测试";
echo $conv->decode($html, 0);
?>
mb_convert_encoding($str, 'GBK', 'UTF-8') 或 iconv()。