2016/05/23

PHP去除UTF-8格式BOM


直接上代码:

<?php

// 设置程序执行时间
set_time_limit(120);
// 文件目录
$path = '.';
if (isset($argv[1])) {
    $path = $argv[1];
}
// 是否删除 BOM
$remove_bom = TRUE;
// 忽略文件
$ignore_file = array(".", "..", ".svn", ".git");
// 统计检索数量
$check_num = 0;
// 统计删除bom数量
$check_del_bom_num = 0;

// 重写文件内容
function rewrite($filename, $data) {
    $filenum = fopen($filename, "w");
    flock($filenum, LOCK_EX);
    fwrite($filenum, $data);
    fclose($filenum);
}

// 检查文件BOM
function checkBOM($filename, $remove_bom=FALSE) {
    GLOBAL $check_del_bom_num;
    $contents = file_get_contents($filename);
    $charset[1] = substr($contents, 0, 1);
    $charset[2] = substr($contents, 1, 1);
    $charset[3] = substr($contents, 2, 1);
    if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
        if ($remove_bom == TRUE) {
            $rest = substr($contents, 3);
            rewrite($filename, $rest);
            $check_del_bom_num = $check_del_bom_num + 1;
            echo "filename: $filename\n";
        }
    }
}

// 递归检索目录下文件的BOM
function checkdir($path, $remove_bom, $ignore_file) {
    GLOBAL $check_num;
    if ($dh = opendir($path)) {
        while (($file = readdir($dh)) !== false) {
            if (!in_array($file, $ignore_file)) {
                if (!is_dir($path . "/" . $file)) {
                    $check_num = $check_num + 1;
                    checkBOM("$path/$file");
                } else {
                    $dirname = $path . "/" . $file;
                    checkdir($dirname, $remove_bom, $ignore_file);
                }
            }
        }
        closedir($dh);
    }
}

checkdir($path, $remove_bom, $ignore_file);
echo "统计检索数量: $check_num, 统计删除bom数量: $check_del_bom_num\n";