代码文件的编码不统一导致的坑
联想到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
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;
}
重新认识一下此问题,当时版主回复的时候我就觉得mb函数里一定有这样的功能,但今日研究了mb库,并没有这样的功能。用mb_detect_encoding总是不准确。
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')));
“第一位字节”使用0xA1-0xFE
“第二位字节”使用0xA1-0xFE
<?phpheader("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) . '<br />';$i = 0;foreach ($all_dirs as $dir_key => $file_path) {$file_content = file_get_contents($file_path);$i++;echo $i . '、' . $file_path.'<br />';var_dump($file_encode_type = mb_detect_encoding($file_content, array('UTF-8', 'EUC-CN'), true)); //EUC-CN是gb2312的另外称呼,php这个扩展返回的是值,不是gb2312echo '<br />';//获取文件的后缀,指定文件类型采取做操作,比如图片就不能去修改的$file_name = basename($file_path);$suffix = get_file_suffix($file_name);if (in_array($suffix, $suffix_list)) {if ($file_encode_type == 'EUC-CN') {echo '<font color="red">had changed the file from ' . $file_encode_type . '(gb2312) to UTF-8:' . $file_path . '</font><br /><br />';//就是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 '<font color="red">the file not in allow file type:' . $file_path . '</font><br /><br />';}unset($file_content);echo '--------------------------------------------------------------------<br /><br />';} }/** +----------------------------------------------------------------------* //搜索指定目录下指定编码的文件,目的就是帮助我们看出自己的项目哪些是什么编码的文件* +----------------------------------------------------------------------*/ function dir_file_encode_list($dir) {$all_dirs = explorer_dir($dir);echo 'get files count:';echo count($all_dirs) . '<br />';$i = 0;foreach ($all_dirs as $dir_key => $file_path) {$file_content = file_get_contents($file_path);$i++;echo $i . '、' . $file_path.'<br />';var_dump($file_encode_type = mb_detect_encoding($file_content, array('UTF-8', 'EUC-CN'), true)); //EUC-CN是gb2312的另外称呼,php这个扩展返回的是值,不是gb2312echo '<br />';unset($file_content);echo '--------------------------------------------------------------------<br /><br />';} } /** +----------------------------------------------------------------------* 扫描指定目录下的指定目录下的html文件,批量将里面的* <meta http-equiv="Content-Type" content="text/html; charset=gbk" />* 指定的编码替换成另外一个编码* +----------------------------------------------------------------------*/ function replace_html_charset($dir) {$all_dirs = explorer_dir($dir);$suffix_list = array('htm', 'html','php');echo 'get files count:';echo count($all_dirs) . '<br />';$i = 0;$charset = '<meta http-equiv="Content-Type" content="text/html; charset=gbk" />';$to_charset = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';foreach ($all_dirs as $dir_key => $file_path) {$file_content = file_get_contents($file_path);$i++;echo $i . '、' . $file_path.'<br />';//获取文件的后缀,指定文件类型采取做操作,比如图片就不能去修改的$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 . '<br /><br />';}}else{echo '<font color="red">the file not in allow file type:' . $file_path . '</font><br /><br />';}} }//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
转载于:https://www.cnblogs.com/wangtao_20/p/4765639.html
代码文件的编码不统一导致的坑相关推荐
- php文件代码采用UTF-8编码的BOM问题---转自CSDN---十方地藏---http://blog.csdn.net/webdesman...
一个项目,代码编码均为utf-8编码,发现问题:在使用session的时候报错,错误如下: ------------------------------------------------- Warn ...
- Python 技术篇 - 通过代码查看文本的编码类型实例演示,如何查看文件的编码类型,文件编码查看方法
文件主要分为二进制文件和文本文件这两种,看你想要查看哪种文件的编码,如果是文本文件的话,open 函数里的就要用 r,二进制文件用的是 rb,别搞错哦! 文本编码查看方法 我们所用的是 chardet ...
- Azure DevOps Server (TFS)中代码文件换行问题解决方案(Git)
之前写过一篇博客"探索TFS Git 库文件换行(CRLF)的处理方式",主要是针对TFVC代码库的. 下面这篇文章说明如何在TFS的Git库中处理代码换行的问题. 概述 在Azu ...
- php中统一编码语句,统一编码
1.目前处理的文件类型有两种 txt html 2.文件的编码未知 要读取文件内容 内容编码最后统一转换成utf-8 最终入库 目前想用mb_convert_encoding($file_conten ...
- linux ll以编码格式_在Linux中文件的编码及对文件进行编码转换
如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...
- python 循环写文件_循环-读写文件-字符编码
目录: 1.1 while与for循环 1.赋值魔法 #1. 序列解包: 将多个值的序列解开,然后放到序列的变量中. x,y,z = 1,2,3 print(x,y,z) #the result : ...
- csv转为utf8编码_中文的csv文件的编码改成utf8的方法
直奔主题:把包含中文的csv文件的编码改成utf-8的方法: 啰嗦几句: 在用pandas读取hive导出的csv文件时,经常会遇到类似UnicodeDecodeError: 'gbk' codec ...
- python代码库-python 编码库
广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 目前我们公司的自动化,大部分是以 python 语言为主,但是经常发现, ...
- 编码中统一更该变量的快捷键_更多项目想法,以提高您的编码技能
编码中统一更该变量的快捷键 Two weeks ago I published an article containing 15 project ideas that you can build to ...
最新文章
- c语言不能写入文件,求大神看看为什么不能将数据写入文件
- android file 创建时间,获得文件的创建时间(精确到时分秒)
- 【PC工具】github项目辅助下载工具,github高速下载
- springAop和AspectJ的关系
- 中石油训练赛 - Bouldering(最短路+剪枝)
- Linux Intel网卡IGB驱动修改mac地址
- pandas 选取第一行_用pandas中的DataFrame时选取行或列的方法
- 新Linux木马发现:每30秒截图并用麦克风录音
- 关于PHP代码写的下载文件打不开的问题,自己备忘!(韩老师2011年的例子)
- Java反射初探 ——“当类也学会照镜子”
- 两个数和三个数的最大值和最小值宏定义
- CentOS 下编译安装AliSQL
- wps是用python语言开发的吗_wps是用什么语言开发的
- 贵州省相关GIS公司或单位
- 观察者模式(行为型)
- (排序5)快速排序(Hoare,选key的随机数与三数取中优化,挖坑法与前后指针法等)
- 云服务器怎么做成文件共享系统,怎样实现云服务器文件共享
- 伟景行Citymaker高亮图层的内元素CSharp
- 实战一:爬取拉勾网职位信息
- booth乘法器的原理与verilog实现