Content-Type 未指定 charset 或與實際不符。<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>中文標題正常顯示</title>
</head>
放在 <title> 之前,確保瀏覽器解析標題時已知編碼。
<?php
// public/index.php 或框架啟動文件最頂部
header('Content-Type: text/html; charset=UTF-8');
find . -name "*.php" -exec iconv -f GBK -t UTF-8 {} -o {}.utf8 \;。<?php
// PDO
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'user', 'pass');
$pdo->exec("SET NAMES utf8mb4");
// mysqli
$mysqli->set_charset('utf8mb4');
注意:使用 utf8mb4 支援 Emoji,表結構也要 CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>頁面標題</title>
</head>
| 檢查點 | 命令/操作 | 正常結果 |
|---|---|---|
| HTTP 響應頭 | curl -I https://example.com | Content-Type: text/html; charset=UTF-8 |
| 文件編碼 | file -bi your.php | text/x-php; charset=utf-8 |
| 數據庫連接字符集 | SHOW VARIABLES LIKE 'character_set%'; | 均為 utf8mb4 |
| 表/列字符集 | SHOW CREATE TABLE users; | CHARSET=utf8mb4 |
| 瀏覽器渲染編碼 | F12 → Network → Response Headers / Document → charset | UTF-8 |
<meta charset> 和 <meta http-equiv> 衝突聲明。AddDefaultCharset utf-8 (Apache) 或 charset utf-8 (Nginx) 作為唯一手段,PHP 應主動發送 Header。iconv、mb_convert_encoding。