大家在使用notePadd++ 或者 EditPlus 打开文件时,经常因为编码问题,导致乱码,尤其是UTF-8的BOM头,下面就提供一个方法检测并去除BOM头。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
function checkBOM($filename){ header('content-type:text/html;charset=utf-8'); if(!file_exists($filename)) exit('请输入正确的文件路径名称!'); $content = ''; $charset = array(); $content = @file_get_contents($filename); $charset[1] = substr($content, 0, 1); $charset[2] = substr($content, 1, 1); $charset[3] = substr($content, 2, 1); if(ord($charset[1]) == 239 && ord($charset[2])==187 && ord($charset[3])==191){ $content = substr($content,3); @file_put_contents($filename, $content); return true; } return false; }
|