萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 檢測文本文件編碼並自動轉 UTF-8

在 Linux 環境下處理上傳的文本文件(如 TXT、CSV、日誌)時,常遇到編碼不一(GBK、BIG5、UTF-16 等)導致亂碼。PHP 內建 mb_detect_encoding() 可靠性有限,本文提供一個基於 BOM 檢測 + 多編碼嘗試驗證 的健壯檢測方案,並整合自動轉碼流程。

編碼檢測器類

<?php
class EncodingDetector {
    private static $priorityEncodings = [
        'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-32LE', 'UTF-32BE', // 優先檢測 Unicode 族
        'GBK', 'GB2312', 'CP936',                                 // 簡體中文常見
        'BIG5', 'CP950',                                          // 繁體中文常見
        'ISO-8859-1', 'ASCII',                                    // 西歐/純英文
    ];
    
    /**
     * 檢測文件編碼
     * @param string $filePath 文件路徑
     * @param int $sampleBytes 讀取樣本位元組數(預設 64KB)
     * @return string|false 編碼名稱或 false(無法確定)
     */
    public static function detectFile(string $filePath, int $sampleBytes = 65536) {
        $handle = fopen($filePath, 'rb');
        if (!$handle) return false;
        
        // 1. 讀取樣本
        $sample = fread($handle, $sampleBytes);
        fclose($handle);
        if ($sample === '') return 'ASCII'; // 空文件
        
        // 2. BOM 直接判斷(最高優先級)
        $bom = self::detectBOM($sample);
        if ($bom) return $bom;
        
        // 3. 多編碼嘗試驗證
        foreach (self::$priorityEncodings as $enc) {
            if (self::isValidEncoding($sample, $enc)) {
                return $enc;
            }
        }
        
        // 4. 兜底:mb_detect_encoding 輔助
        $detected = mb_detect_encoding($sample, self::$priorityEncodings, true);
        return $detected ?: false;
    }
    
    /**
     * 檢測 BOM
     * @return string|false 'UTF-8'|'UTF-16LE'|'UTF-16BE'|'UTF-32LE'|'UTF-32BE'|false
     */
    private static function detectBOM(string $sample): string|false {
        $boms = [
            "\xEF\xBB\xBF" => 'UTF-8',
            "\xFF\xFE"     => 'UTF-16LE',
            "\xFE\xFF"     => 'UTF-16BE',
            "\xFF\xFE\x00\x00" => 'UTF-32LE',
            "\x00\x00\xFE\xFF" => 'UTF-32BE',
        ];
        foreach ($boms as $bytes => $enc) {
            if (strncmp($sample, $bytes, strlen($bytes)) === 0) {
                return $enc;
            }
        }
        return false;
    }
    
    /**
     * 驗證字符串在指定編碼下是否合法
     */
    private static function isValidEncoding(string $str, string $encoding): bool {
        // 1. 圖騰:轉碼後再轉回,比較是否一致(忽略不可轉換字符)
        $converted = @mb_convert_encoding($str, 'UTF-8', $encoding);
        if ($converted === false) return false;
        $back = @mb_convert_encoding($converted, $encoding, 'UTF-8');
        if ($back === false) return false;
        
        // 2. 允許少量不可轉換字符(如孤立 surrogate),但比例不超過 0.1%
        $len = mb_strlen($str, $encoding);
        $len2 = mb_strlen($converted, 'UTF-8');
        if ($len === 0) return true;
        $lossRate = abs($len - $len2) / $len;
        return $lossRate < 0.001;
    }
    
    /**
     * 將文件轉換為 UTF-8 並保存
     * @param string $sourcePath 源文件
     * @param string $targetPath 目標文件(可同源覆蓋)
     * @param string|null $fromEncoding 指定源編碼,null 自動檢測
     * @return bool
     */
    public static function convertToUtf8(string $sourcePath, string $targetPath, ?string $fromEncoding = null): bool {
        $encoding = $fromEncoding ?? self::detectFile($sourcePath);
        if (!$encoding) return false;
        if (strtoupper($encoding) === 'UTF-8') {
            // 已是 UTF-8,直接複製或移動
            return copy($sourcePath, $targetPath);
        }
        
        $handleIn = fopen($sourcePath, 'rb');
        $handleOut = fopen($targetPath, 'wb');
        if (!$handleIn || !$handleOut) return false;
        
        // 寫入 UTF-8 BOM(可選,視下游系統需求)
        // fwrite($handleOut, "\xEF\xBB\xBF");
        
        $bufferSize = 8192;
        while (!feof($handleIn)) {
            $chunk = fread($handleIn, $bufferSize);
            $converted = @mb_convert_encoding($chunk, 'UTF-8', $encoding);
            if ($converted === false) {
                fclose($handleIn);
                fclose($handleOut);
                @unlink($targetPath);
                return false;
            }
            fwrite($handleOut, $converted);
        }
        
        fclose($handleIn);
        fclose($handleOut);
        return true;
    }
}
?>

使用示例

<?php
require_once 'EncodingDetector.php';

$uploadFile = '/tmp/upload_123.txt';

// 1. 檢測編碼
$encoding = EncodingDetector::detectFile($uploadFile);
echo "檢測編碼: " . ($encoding ?: '未知') . "\n";

// 2. 轉換為 UTF-8
$utf8File = '/tmp/upload_123_utf8.txt';
if (EncodingDetector::convertToUtf8($uploadFile, $utf8File)) {
    echo "轉換成功: $utf8File\n";
    // 後續可用 LibreOffice/PDF 轉換工具處理
} else {
    echo "轉換失敗,請人工檢查\n";
}
?>

整合到文檔轉換流程

你提到的流程:TXT --(OpenOffice/LibreOffice)--> PDF --(swftools)--> SWF,關鍵點在於 LibreOffice 需輸入 UTF-8 編碼文件。完整流程腳本:

<?php
function convertTxtToSwf(string $txtPath, string $outputDir): ?string {
    // 1. 編碼轉換
    $utf8Txt = $outputDir . '/source_utf8.txt';
    if (!EncodingDetector::convertToUtf8($txtPath, $utf8Txt)) {
        error_log("編碼轉換失敗: $txtPath");
        return null;
    }
    
    // 2. LibreOffice 轉 PDF(需安裝 libreoffice,支援無頭模式)
    $pdfPath = $outputDir . '/output.pdf';
    $cmd = "libreoffice --headless --convert-to pdf --outdir " . escapeshellarg($outputDir) . " " . escapeshellarg($utf8Txt);
    exec($cmd, $out, $ret);
    if ($ret !== 0 || !file_exists($pdfPath)) {
        error_log("LibreOffice 轉 PDF 失敗: " . implode("\n", $out));
        return null;
    }
    
    // 3. swftools pdf2swf 轉 SWF
    $swfPath = $outputDir . '/output.swf';
    $cmd = "pdf2swf " . escapeshellarg($pdfPath) . " -o " . escapeshellarg($swfPath) . " -T 9 -f";
    exec($cmd, $out, $ret);
    if ($ret !== 0 || !file_exists($swfPath)) {
        error_log("pdf2swf 失敗: " . implode("\n", $out));
        return null;
    }
    
    // 4. 清理臨時文件
    @unlink($utf8Txt);
    @unlink($pdfPath);
    
    return $swfPath;
}
?>

編碼檢測的局限性與補充

  • 短文本誤判率高:不足 100 字符的文本極易誤判。建議:強制要求用戶指定編碼,或提供預覽讓用戶確認。
  • GBK/GB2312/CP936 重疊:這三者關係密切,檢測常返回 GBK。實際處理時統一按 GBK/CP936 處理即可。
  • 混合編碼文件:單一檢測無法處理。需按行/塊檢測,或人工干預。
  • 性能:大文件(>100MB)全量檢測較慢。只讀取首 64KB + 尾 64KB + 中間隨機抽樣可大幅加速。

替代工具推薦

  • file -i (Linux 命令):基於 magic 數據庫,檢測準確率高,可 exec('file -bi ' . escapeshellarg($file)) 調用。
  • uchardet (C++ 庫/命令行):Mozilla 專案,支援超多編碼,準確度業界領先。
  • PHP 擴展 php-uchardet:直接在 PHP 內調用 uchardet,效能最佳。
copyright © 萬盛學電腦網 all rights reserved