php和bom头的冤仇 和 如何在linux下使用命令去掉bom头

事情是这样的 

我有个php文件的功能是读取一个二进制文件并且echo出来,该文件本来是图片文件,但是我访问这个php文件并且写入到本地硬盘的时候发现并不是图片格式 

用hexdump 查看的时候发现文件首部多了3个字节
,这不刚好就是bom的utf8编码的十六进制表示么,后来找到原因是因为有个同事包含了一个使用了bom头的php文件导致,该字节在文本编辑器里是不可见的,着实增加了调试难度。


导致这个问题的原因是  bom头是在php的标记<php?>之外的 它的结构类似是这样的


ef bb bf 
<?php
echo111;
?>

因为php会把标签外的内容原封不动的输出,所以导致了这个问题。解决办法就是去掉该php文件的bom头


去除方法:

在windows上如果使用的是phpstom的话,在file菜单下面的有 remove bom的菜单项

这里说下linux使用命令取出Bom的方法


对已经是utf8编码并且拥有bom的文件去掉bom: sed -i '1s/^\xef\xbb\xbf//' 11.php

相反的
给已经是utf8编码得文件 添加bom  :sed -i '1s/^/\xef\xbb\xbf/' 11.php

需要注意的是
这两条命令只针对 utf8编码的文件  对utf16编码的文件不能使用 ,因为bom的utf8的编码表示为ef bb bf三个字节,但是在utf16编码下并不是

bom在utf16编码下的作用是为了说明该文件存储的字节序 ,关于bom的具体作用可以参看wiki,wiki上面已经说的很详细了


reference:http://muzso.hu/2011/11/08/using-awk-sed-to-detect-remove-the-byte-order-mark-bom

{以下是链接内容  防止链接失效}
http://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-ma...
BOM  is nothing more than a nuisance. Detecting and removing it is plain simple. There's a utility called  bomstrip  that pretty much does what is says and there's the one-liner AWK implementation that you find on stackoverflow (I've added whitespaces for better readability):
awk '{ if (NR == 1) sub(/^\xef\xbb\xbf/, ""); print }' INFILE > OUTFILE
Or a sed version (replaces in place):
sed -i -e '1s/^\xEF\xBB\xBF//' FILE
To do this (or just simply detect BOM) recursively in a dir, you'll find tons of ideas  in this other stackoverflow  article.

For recursively detecting files with a BOM, I prefer this one:
find . -type f -print0 | xargs -0r awk '/^\xEF\xBB\xBF/ {print FILENAME} {nextfile}'

To recursively remove BOM, you could use something like this:
find . -type f -exec sed -i -e '1s/^\xEF\xBB\xBF//' {} \;

Or a slightly safer variant:
find . -type f -exec sed -i.bak -e '1s/^\xEF\xBB\xBF//' {} \; -exec rm '{}.bak' \;
{以上是链接内容  防止链接失效}

你可能感兴趣的:(php和bom头的冤仇 和 如何在linux下使用命令去掉bom头)