2 月 272016
 

Source: UTF-8編碼中BOM的檢測與刪除

所謂BOM,全稱是Byte Order Mark,它是一個Unicode字符,通常出現在文本的開頭,用來標識字節序(Big/Little Endian),除此以外還可以標識編碼(UTF-8/16/32 ),如果出現在文本中間,則解釋為zero width no-break space注:Unicode相關知識的詳細介紹請參考UTF-8, UTF-16, UTF-32 & BOM。 對於UTF-8/16/32而言,它們名字中的8/16/32指的是編碼單位是多少位的,也就是說,它們的編碼單位分別是8/16/32位,換算成字節就 是1/2/4字節,如果是多字節,就要牽扯到字節序,UTF-8以單字節為編碼單位,所以不存在字節序。UTF-8主要的優點是可以兼容ASCII,但如 果使用BOM的話,這個好處就蕩然無存了,除此以外,BOM的存在還可能引發一些問題,比如下面錯誤便都有可能是BOM導致的:

  • Shell: No such file or directory
  • PHP: Warning: Cannot modify header information – headers already sent

在詳細討論UTF-8編碼中BOM的檢測與刪除問題前,不妨先通過一個例子熱熱身:

shell> curl -s http://phone.10086.cn/ | head -1 | sed -nl
\357\273\277<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional\
//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r$

如上所示,前三個字節分別是357、273、277,這就是八進制的BOM。

shell> curl -s http://phone.10086.cn/ | head -1 | hexdump -C
00000000 ef bb bf 3c 21 44 4f 43 54 59 50 45 20 68 74 6d |...<!DOCTYPE htm|
00000010 6c 20 50 55 42 4c 49 43 20 22 2d 2f 2f 57 33 43 |l PUBLIC "-//W3C|
00000020 2f 2f 44 54 44 20 58 48 54 4d 4c 20 31 2e 30 20 |//DTD XHTML 1.0 |
00000030 54 72 61 6e 73 69 74 69 6f 6e 61 6c 2f 2f 45 4e |Transitional//EN|
00000040 22 20 22 68 74 74 70 3a 2f 2f 77 77 77 2e 77 33 |" "http://www.w3|
00000050 2e 6f 72 67 2f 54 52 2f 78 68 74 6d 6c 31 2f 44 |.org/TR/xhtml1/D|
00000060 54 44 2f 78 68 74 6d 6c 31 2d 74 72 61 6e 73 69 |TD/xhtml1-transi|
00000070 74 69 6f 6e 61 6c 2e 64 74 64 22 3e 0d 0a |tional.dtd">..|

如 上所示,前三個字節分別是EF、BB、BF,這就是十六進制的BOM。注:用到了第三方網站的頁面,不能保證例子始終可用。實際做項目開發時,可能會面對 成百上千個文本文件,如果有幾個文件混入了BOM,那麼很難察覺,如果沒有帶BOM的UTF-8文本文件,可以用vi杜撰幾個,相關命令如下:

設置UTF-8編碼:

:set fileencoding=utf-8

添加BOM:

:set bomb

刪除BOM:

:set nobomb

查詢BOM:

:set bomb?

如何檢測UTF-8編碼中的BOM呢?

shell> grep -r -I -l $'^\xEF\xBB\xBF' /path

如何刪除UTF-8編碼中的BOM呢?

shell> grep -r -I -l $'^\xEF\xBB\xBF' /path | xargs sed -i 's/^\xEF\xBB\xBF//;q'

推薦:如果你使用SVN的話,可以在pre-commit鉤子裡加上相關代碼用以杜絕BOM。

#!/bin/bash

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook

for FILE in $($SVNLOOK changed -t "$TXN" "$REPOS" | awk '/^[AU]/ {print $NF}'); do
    if $SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | grep -q $'^\xEF\xBB\xBF'; then
        echo "Byte Order Mark be found in $FILE" 1>&2
        exit 1
    fi
done

 

請注意此指令將會把檔案中第一行之外的全數清除。

grep -r -I -l $’^\xEF\xBB\xBF’ /path | xargs sed -i ‘s/^\xEF\xBB\xBF//;q’

可用以下指令嘗試之:

grep -r -I -l $’^\xEF\xBB\xBF’ /path | xargs sed -i ‘s/^\xEF\xBB\xBF//g’

 

 

  One Response to “UTF-8編碼中BOM的檢測與刪除”

  1. Notepad 存utf8時會在檔案前面加3個 byte的 bom

    用android studio 寫java時很困擾

    的破解

    系統 windows 10 64 bit

    takeown /f notepad.exe
    icacls notepad.exe /grant everyone:F

    32 bit
    C:\Windows\sysWOW64\notepad.exe
    207,872 Bytes
    6660 : 00 00 83 E8 01 0F 84 9E-00 00 00 83 E8 01 75 1E
    6660 : — — — — — — — — — — — — — — EB —
    64 bit
    C:\Windows\system32\notepad.exe
    215,040 Bytes
    3FA0 : 83 F9 01 75 2B
    3FA0 : — — — EB — 

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

CAPTCHA Image
Play CAPTCHA Audio
Reload Image