0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BOM付きUTF-8

Posted at

BOM付きUTF-8とは

バイトオーダーマーク (英: byte order mark) あるいはバイト順マーク(バイトじゅんマーク)は通称BOM(ボム)といわれる、Unicodeの符号化形式で符号化したテキストの先頭につける数バイトのデータのことである。このデータを元にUnicodeで符号化されていることおよび符号化の種類の判別に使用する。(出典:wikipedia)

BOMのせいで何が起こるか

UTF-8形式などのテキストファイルを読み込んだ際、BOMがついていることによって、先頭の文字列のみ長さが長くなります。
これはvar_dumpなどで表示させることはできないので、かなり厄介です。
文字列で扱う場合は取り除いておく必要があります。

取り除くには

phpでは次のようにして取り除けます。

$str = preg_replace('/^\xEF\xBB\xBF/', '', $str);

まとめ

UTF-8形式のテキストファイルを読み込んだ際に最初の文字列だけ不可解な挙動をする場合はBOMを疑ってみてください。
気づくことができると、取り除くことは簡単にできます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?