代码文件的编码不统一导致的坑

 

 

 

联想到discuz,ecshop发布一个新版本的系统给大家使用,会提供utf-8,gb2312版本的代码下载。所以肯定是批量转换编码出来的。

 

 

这种是转换html文件。

 

http://blog.csdn.net/iseagold/article/details/5472377

 

我需要找一个批量转换文件编码的工具才行。

 

 

问题在于:目录中混合了gb2312和utf8编码的。如何才能用工具去自动判断呢。

不要强制转换一次编码。

gb2312和utf8编码的。如果是utf8编码的文件,就不要转换,如果是gb2312的编码才执行转换成utf8编码。

 

 

http://blog.csdn.net/liu_qiqi/article/details/38706497

 

按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以, 基本的原理就是只要读出文件前三个字节,判定这些字节的值,就可以得知其编码的格式。其实,如果项目运行的平台就是中文操作系统, 如果这些文本文件在项目内产生,即开发人员可以控制文本的编码格式,只要判定两种常见的编码就可以 了:GBK和UTF-8。由于中文Windows默认的编码是GBK,所以一般只要判定UTF-8编码格式。
   对于UTF-8编码格式的文本文件, 其前3个字节的值就是-17、-69、-65,所以,判定是否是UTF-8编码格式的代码片段如下:
 
 
 
 
 
 
 
 
 
 
 
 

php的mb_detect_encoding函数,我正准备试一试:

function characet($data){

  if( !empty($data) ){   

    $filetype = mb_detect_encoding($data , array('utf-8','gbk','latin1','big5')) ;  

    if( $filetype != 'utf-8'){  

      $data = mb_convert_encoding($data ,'utf-8' , $filetype);  

    }  

  }  

  return $data;   

}

 

 
 
 
 http://blog.csdn.net/wydycrtd/article/details/4793124
 
 
有人也说了,

重新认识一下此问题,当时版主回复的时候我就觉得mb函数里一定有这样的功能,但今日研究了mb库,并没有这样的功能。用mb_detect_encoding总是不准确。
 
 
 
mbstring 当前实现了以下编码检测筛选器。 如有以下编码列表的无效字节序列,编码的检测将会失败。

UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP


 var_dump(mb_detect_encoding($str,array('UTF-8','GB2312')));
 
 
 
 
EUC-CN EUC-CN是GB2312最常用的表示方法。浏览器编码表上的“GB2312”,通常都是指“ EUC-CN”表示法。
 
php中用mb_detect_encoding测出来的euc-cn是gb2312编码:EUC-CN是GB2312最常用的表示方法
 
 
 
GB 2312字元使用两个字节来表示。

“第一位 字节”使用0xA1-0xFE
“第二位字节”使用0xA1-0xFE
 
 
网上找到的不能解决自动检测编码的问题,这里我根据自己需要。检测两种编码就可以了:gb2312和utf-8
php

header("Content-type: text/html; charset=utf-8");

/*
 * +---------------------------------------------------
 *  遍历指定目录,形成一个目录句柄返回
 * +---------------------------------------------------
 *
 * +---------------------------------------------------
 */

function explorer_dir($sDir) {

    static $aTempArr = array();

    $dp = opendir($sDir);

    while ($sFileName = readdir($dp)) {
        if ($sFileName != '.' && $sFileName != '..') {
            $sPath = $sDir . "/" . $sFileName;
            if (is_dir($sPath)) {
                explorer_dir($sPath);
            } else {
//                $filetime=date("Y-m-d H:i:s",filectime("$path"));
//                $fp=$path.",".$filetime;
                $fp = $sPath;
                $aTempArr[] = $fp;
            }
        }
    }
    closedir($dp);
    return $aTempArr;
}
/*
 * +----------------------------------------------------------------------
 * //搜索指定目录下的gb2312编码文件,转换成utf-8编码文件
 * +----------------------------------------------------------------------
 */
function change_gb2312_to_utf8_dir($dir) {

    $all_dirs = explorer_dir($dir);
    $suffix_list = array('php', 'htm', 'html', 'txt', 'js');
    echo 'get files count:';
    echo count($all_dirs) . '
'; $i = 0; foreach ($all_dirs as $dir_key => $file_path) { $file_content = file_get_contents($file_path); $i++; echo $i . '、' . $file_path.'
'; var_dump($file_encode_type = mb_detect_encoding($file_content, array('UTF-8', 'EUC-CN'), true)); //EUC-CN是gb2312的另外称呼,php这个扩展返回的是值,不是gb2312 echo '
'; //获取文件的后缀,指定文件类型采取做操作,比如图片就不能去修改的 $file_name = basename($file_path); $suffix = get_file_suffix($file_name); if (in_array($suffix, $suffix_list)) { if ($file_encode_type == 'EUC-CN') { echo 'had changed the file from ' . $file_encode_type . '(gb2312) to UTF-8:' . $file_path . '

'; //就是gb2312编码的 $after_change_encode_content = iconv("gb2312", "UTF-8", $file_content); unlink($file_path); file_put_contents($file_path, $after_change_encode_content); unset($after_change_encode_content); } } else { echo 'the file not in allow file type:' . $file_path . '

'; } unset($file_content); echo '--------------------------------------------------------------------

'; } } /* * +---------------------------------------------------------------------- * //搜索指定目录下指定编码的文件,目的就是帮助我们看出自己的项目哪些是什么编码的文件 * +---------------------------------------------------------------------- */ function dir_file_encode_list($dir) { $all_dirs = explorer_dir($dir); echo 'get files count:'; echo count($all_dirs) . '
'; $i = 0; foreach ($all_dirs as $dir_key => $file_path) { $file_content = file_get_contents($file_path); $i++; echo $i . '、' . $file_path.'
'; var_dump($file_encode_type = mb_detect_encoding($file_content, array('UTF-8', 'EUC-CN'), true)); //EUC-CN是gb2312的另外称呼,php这个扩展返回的是值,不是gb2312 echo '
'; unset($file_content); echo '--------------------------------------------------------------------

'; } } /* * +---------------------------------------------------------------------- * 扫描指定目录下的指定目录下的html文件,批量将里面的 * * 指定的编码替换成另外一个编码 * +---------------------------------------------------------------------- */ function replace_html_charset($dir) { $all_dirs = explorer_dir($dir); $suffix_list = array('htm', 'html','php'); echo 'get files count:'; echo count($all_dirs) . '
'; $i = 0; $charset = ''; $to_charset = ''; foreach ($all_dirs as $dir_key => $file_path) { $file_content = file_get_contents($file_path); $i++; echo $i . '、' . $file_path.'
'; //获取文件的后缀,指定文件类型采取做操作,比如图片就不能去修改的 $file_name = basename($file_path); $suffix = get_file_suffix($file_name); if (in_array($suffix, $suffix_list)) { $patten = '%' . $charset . '%i'; if (preg_match($patten, $file_content)) { $after_change_encode_content = str_ireplace($charset, $to_charset, $file_content); unlink($file_path); file_put_contents($file_path, $after_change_encode_content); unset($after_change_encode_content); echo 'find limit :' . $file_path . '

'; } }else{ echo 'the file not in allow file type:' . $file_path . '

'; } } } //dir_file_encode_list("D:\\static\\develop\\mama\\test_change_encode"); //change_gb2312_to_utf8_dir("D:\\static\\develop\\mama\\test_change_encode"); //replace_html_charset("D:\\static\\develop\\mama\\test_change_encode"); function get_file_suffix($file_name){ $file_name_arr = explode(".", $file_name); $suffix = array_pop($file_name_arr); return $suffix; }
View Code

 



 

你可能感兴趣的:(代码文件的编码不统一导致的坑)