close

programing-languages-2-1242049.jpg

(圖片來源)

 

某次我利用 php Curl 來呼叫 WordPress API ,透過 php strlen function 查看回傳的 123 字串長度,印出在網頁上時卻顯示有8個字元,打開 Chrome 開發者工具查看元素才發現回傳的值包含  的字串,這令我相當的困惑,如下圖:

 

google 搜尋 “&#65279 php" 後發現其實還蠻多人有遇到一樣的問題!會有這樣的問題發生,是因為網站專案中有使用到 UTF8 含 BOM 編碼的檔案,那究竟什麼是 BOM 呢? BOM 的全名為 Byte Order Mark,詳細的說明可以自己去瞭解,這篇文章主要的目的是在如何解決因 BOM  而產生的問題。

 

解決方案:

  • Notepad++
    • 找到該檔案並用 Notepad++ 開啟
    • 點擊 encoding 並改成 UTF-8 的編碼
  • php code
    • 新增 removeBOM.php 檔案
    • 輸入檢查 BOM 的網址

 

一、 Notepad++

在網路上找到最常見的解法就是將該檔案利用 Notepad++ 編輯器來進行編碼的轉換,轉換方式如下:

1. 找到該檔案並用 Notepad++ 開啟

2. 點擊 encoding 並改成 UTF-8 的編碼

看起來好像沒什麼大不了,是個很容易解決的問題,但當你依照網路上的解法來解決的時候馬上就會遇到一個非常大的問題,那就是到底是哪個檔案為 UTF8 BOM 編碼?尤其是當你使用的是一個框架,幾乎沒什麼人分享到底該如何找到有該編碼的檔案,此時你可以參考第二個方案來解決問題!

 

二、php code

1 新增 removeBOM.php 檔案

首先在網站根目錄新增 removeBOM.php 檔案,並將下方的程式碼填入其中:

<?php

//remove the utf-8 boms
//by magicbug at gmail dot com

if (isset($_GET['dir'])){ //config the basedir
	$basedir=$_GET['dir'];
}else{
  	$basedir = '.';
}

$auto = 1;

checkdir($basedir);

/**
 * [checkdir 檢查目錄]
 * @param  [type]                   $basedir [description]
 * @return [type]
 * @date   2017-10-29T19:01:11+0800
 */
function checkdir($basedir)
{
  	if ($dh = opendir($basedir)) {
    	while (($file = readdir($dh)) !== false) {
      		if ($file != '.' && $file != '..'){
        		if (!is_dir($basedir."/".$file)) {
          			echo "filename $basedir/$file ".checkBOM("$basedir/$file")." <br>";
        		}else{
          			$dirname = $basedir."/".$file;
          			checkdir($dirname);
        		}
      		}
    	}
  		closedir($dh);
  	}
}

/**
 * [checkBOM 檢查檔案BOM]
 * @param  [type]                   $filename [description]
 * @return [type]
 * @date   2017-10-29T19:01:19+0800
 */
function checkBOM ($filename)
{
   	global $auto;
   	$contents = file_get_contents($filename);
   	$charset[1] = substr($contents, 0, 1);
   	$charset[2] = substr($contents, 1, 1);
   	$charset[3] = substr($contents, 2, 1);
   	if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
     	if ($auto == 1) {
     	  	$rest = substr($contents, 3);
     	  	rewrite ($filename, $rest);
     	  	return ("<font color=red>BOM found, automatically removed.</font>");
     	} else {
       		return ("<font color=red>BOM found.</font>");
     	}
   	}
   	else return ("BOM Not Found.");
}

/**
 * [rewrite 移除檔頭BOM]
 * @param  [type]                   $filename [description]
 * @param  [type]                   $data     [description]
 * @return [type]
 * @date   2017-10-29T19:01:40+0800
 */
function rewrite ($filename, $data)
{
   	$filenum = fopen($filename, "w");
   	flock($filenum, LOCK_EX);
   	fwrite($filenum, $data);
   	fclose($filenum);
}

?>

 

2. 輸入檢查 BOM 的網址

輸入下方網址,dirname 可以填入需要檢查的資料夾,若不帶 dir 參數則是整個專案的檔案都會進行檢查。

http://domain/removeBOM.php?dir={dirname}

 

輸入網址後網頁會開始列出所有檔案檢查的狀態,若有檢查到檔案有 UTF8 BOM 編碼會特別以紅字來顯示,並且會自動移除檔頭BOM ,有了這個功能就不用煩惱找不到檔案了呢!

 

參考資料:

php中隐形字符65279(utf-8的BOM头)问题 

 

arrow
arrow
    文章標籤
    bom notepad php 65279 utf8
    全站熱搜

    Mayuge 發表在 痞客邦 留言(0) 人氣()