| 層級 | 現象 | 根因 |
|---|---|---|
| 文件編碼 | 打開舊項目文件顯示亂碼、保存後中文變問號 | 文件實際為 GBK/Big5,PhpStorm 預設 UTF-8 讀取 |
| 項目/IDE 編碼 | 新建文件默認 GBK、Git 提交編碼不一致 | Settings → File Encoding 設置與團隊約定不符 |
| 運行/數據庫編碼 | 頁面顯示正常但入庫亂碼、或讀庫顯示亂碼 | PHP 腳本、資料庫連接、表字符集不一致 |
File → Settings → Editor → File Encodings (或 File → File Properties → File Encoding 舊版)UTF-8UTF-8UTF-8Transparent native-to-ascii conversion (屬性文件轉義)Apply → OK關鍵:若彈出「Convert」對話框,選 Convert 將現有文件實際轉碼;選 Reload 僅改變讀取方式,文件內容不變,亂碼依舊。
# Linux/macOS 終端
find . -type f -name "*.php" -exec sh -c 'iconv -f GBK -t UTF-8 "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;
# Windows PowerShell
Get-ChildItem -Recurse -Filter *.php | ForEach-Object {
$content = Get-Content $_.FullName -Encoding Default
Set-Content $_.FullName -Value $content -Encoding UTF8
}
或使用 PhpStorm Code → Convert File Encoding 逐個轉換(小項目可行)。
<?php
declare(encoding='UTF-8'); // PHP 7.1+ 聲明腳本編碼
header('Content-Type: text/html; charset=UTF-8'); // HTTP 響應頭
mb_internal_encoding('UTF-8'); // 多字節函數內部編碼
mb_http_output('UTF-8'); // 輸出編碼
mb_regex_encoding('UTF-8'); // 正則編碼
// PDO
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", $user, $pass, [
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"
]);
// mysqli
$mysqli->set_charset('utf8mb4');
必須使用 utf8mb4 支援 Emoji/生僻字,utf8 僅支援 BMP 平面。
SHOW VARIABLES LIKE 'character_set%';
SHOW CREATE TABLE `users`; -- 查看表默認字符集
-- 修正庫/表/列
ALTER DATABASE `mydb` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE `users` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE `users` CHANGE `nickname` `nickname` VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# .gitattributes (提交到倉庫)
* text=auto eol=lf
*.php text eol=lf
*.sh text eol=lf
*.bat text eol=crlf
# 全局配置
git config --global core.autocrlf input # 提交時轉 LF,檢出不轉 (Linux/mac)
git config --global core.autocrlf true # Windows: 提交轉 LF,檢出轉 CRLF
git config --global core.quotepath false # 顯示中文文件名不轉義
| 設置路徑 | 推薦值 | 說明 |
|---|---|---|
| Settings → Editor → File Encodings → Global/Project | UTF-8 | 核心 |
| Settings → Editor → General → Console Encoding | UTF-8 | 運行控制台輸出 |
| Settings → Tools → Terminal → Default encoding | UTF-8 | 內建終端 |
| Settings → Build, Execution, Deployment → Database → Drivers → Options → characterEncoding | UTF-8 | 數據庫工具窗口讀寫 |
| Settings → Editor → Inspections → PHP → Encoding issues | 勾選 | 靜態檢測編碼問題 |
| 場景 | 現象 | 排查順序 |
|---|---|---|
| include/require 引入文件 | 引入處輸出亂碼 | 被引文件編碼 → 引入文件編碼 → PHP 內部編碼 |
| json_encode 中文成 \uXXXX | API 返回 Unicode 轉義 | 加 JSON_UNESCAPED_UNICODE 選項 (PHP 5.4+) |
| CSV 導出 Excel 打開亂碼 | Excel 默認用系統編碼打開 | 輸出 UTF-8 with BOM: echo "\xEF\xBB\xBF" . $csv; |
| Shell 命令輸出中文參數 | 參數在 PHP 變亂碼 | escapeshellarg() + 確保終端編碼 UTF-8 |
PhpStorm 中文亂碼本質是 「編碼不一致」。解決路徑:
1. IDE 統一 UTF-8 (Global/Project/Console/Terminal)。
2. 現有文件批量轉碼 (iconv/PhpStorm Convert)。
3. 運行時強制 UTF-8 (header/mb_internal_encoding/PDO charset)。
4. 資料庫全鏈路 utf8mb4 (庫/表/列/連接)。
5. Git 規範換行與編碼 (.gitattributes + core.autocrlf)。
完成以上五步,團隊協作、跨平台部署、多語言存儲的編碼問題將徹底消失。