EF BB BF 三個位元組,標記檔案為 UTF-8 編碼。header()、session_start() 失效 (已輸出內容)| 編輯器 | 預設儲存行為 |
|---|---|
| UltraEdit | 提供「UTF-8」與「UTF-8 - 無 BOM」兩種選項 |
| Windows 記事本 | 預設儲存含 BOM |
| EditPlus 2.31 | 無 BOM |
| EditPlus 2.11 | 含 BOM |
| VS Code / Sublime / PhpStorm | 預設無 BOM (右下角可切換) |
UltraEdit:另存新檔 → 編碼選 UTF-8 - 無 BOM
VS Code:右下角點擊「UTF-8 with BOM」切換為「UTF-8」
將以下程式碼存為 remove_bom.php 放在專案根目錄執行:
isFile()) continue;
$path = $file->getPathname();
$content = file_get_contents($path);
if (strlen($content) >= 3 && ord($content[0]) === 0xEF && ord($content[1]) === 0xBB && ord($content[2]) === 0xBF) {
if ($autoRemove) {
file_put_contents($path, substr($content, 3));
echo "[移除] $path\n";
} else {
echo "[發現 BOM] $path\n";
}
}
}
echo "完成。\n";
ob_start(function ($buffer) {
return preg_replace('/^\xEF\xBB\xBF/', '', $buffer);
});
放在入口檔案最開頭,可移除輸出中的 BOM,但無法解決 header() 已送出的問題。
.gitattributes 加入 * text=auto eol=lf,並在 pre-commit hook 檢查 BOM。