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

{以下是链接内容  防止链接失效}
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' \;
{以上是链接内容  防止链接失效}

在 Java 中,我们可以使用 `java.nio.charset.CharsetDecoder` 类来处理 UTF-16 编码的 BOM 。 下面是一个例子: ```java import java.io.*; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; public class RemoveBOM { public static void main(String[] args) throws IOException { // 打开 UTF-16 编码的文件 FileInputStream fis = new FileInputStream("file.txt"); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-16")); // 创建 CharsetDecoder 对象 CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); // 读取文件内容 CharBuffer cb = CharBuffer.allocate(fis.available()); isr.read(cb); cb.flip(); // 去除 BOM if (cb.length() > 0 && cb.charAt(0) == '\ufeff') { cb.deleteCharAt(0); } // 处理文件内容 // ... // 保存文件内容 FileOutputStream fos = new FileOutputStream("file.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-16")); osw.write(cb.toString()); osw.close(); } } ``` 在这个例子中,我们首先使用 `java.io.FileInputStream` 和 `java.io.InputStreamReader` 打开了一个 UTF-16 编码的文件,并通过 `java.nio.charset.Charset.forName` 方法创建了一个 `java.nio.charset.CharsetDecoder` 对象。 然后,我们使用 `java.nio.charset.CharsetDecoder` 对象读取了文件内容,并检查了它是否以 BOM 。如果是,我们使用 `java.lang.CharSequence.deleteCharAt` 方法从内容中去除了 BOM 。 在处理完文件内容后,我们再次使用 `java.io.FileOutputStream` 和 `java.io.OutputStreamWriter` 将内容写入文件中。这次,由于我们已经去除了 BOM ,所以写入的文件是没有 BOM 的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值