php实现文件编码批量转换


Posted in PHP onMarch 10, 2014

有些问题,不能重复转,比如gbk转到utf8,然后有在转成utf8,这样会乱码,我本来试图在转换之前去检测编码的,貌似失败了。我特意试了一个文件,我检测它是是否是gbk或者是utf-8,都返回true。这就不懂了。

<?php
/**
 * 转换文件编码
 * 依赖的扩展filesystem 和 mbstring
 * @example
 * <pre>
 * include_once 'ConvertEncode.php';
 * $convert = new ConvertEncode();
 * try{
 *   $convert->setPath('my', true, true);//目录
 *    //$convert->setPath('my.php');//单文件
 *   $convert->setEncode('GBK', 'UTF-8');
 *   $convert->convert();
 * }catch(ConvertException $e) {
 *   echo $e->getMessage();
 * }
 * </pre>
 */
class ConvertEncode { /**
  * 要转换成的编码
  * @var string
  */
 private $_to_encoding;
 /**
  * 转换前的编码
  * @var string
  */
 private $_from_encoding;
 /**
  * 要转换的的目录或者单文件
  * @var string
  */
 private $_path;
 /**
  * 是否是一个目录,当给出的是目录是才设置
  * @var boolean
  */
 private $_directory;
 /**
  * 是否递归遍历,仅对目录有效
  * @var boolean
  */
 private $_recursion;
 /**
  * 保存所有待转换的文件,仅当转换目录里面的文件时才用
  * @var array
  */
 private $_files = array();
 /**
  * 构造函数
  */
 public function __construct() {
  if( ! function_exists('mb_convert_encoding') ) {
   throw new ConvertException('mbstring extension be required');
  }
 }
 /**
  * 设置需要转换的目录或者单文件
  * @param string $path 目录或者文件
  * @param boolean 是否是目录
  * @param boolean 是否递归目录
  * @return boolean
  */
 public function setPath($path, $is_dir = false, $rec = false) {
  $this->_path = $path;
  $this->_directory = $is_dir;
  $this->_recursion = $rec;
  return true;
 }
 /**
  * 设置转换前的编码和要转换到的编码
  * @param string $encode 转换前的编码
  * @param string $encode 转换到的编码
  * @return boolean
  */
 public function setEncode($encode_from, $encode_to) {
  $this->_from_encoding = $encode_from;
  $this->_to_encoding   = $encode_to;
  return true;
 }
 /**
  * 转换编码,根据是否是目录的设置分别转换
  * @return boolean
  */
 public function convert() {
  if($this->_directory ) {
   return $this->_convertDirectory();
  }
  return $this->_convertFile();
 }
 /**
  * 转换文件
  * @throws ConvertException
  * @return boolean
  */
 private function _convertFile() {
  if( ! file_exists($this->_path) ) {
   $message = $this->_path . ' does not exist.';
   throw new ConvertException($message);
  }
  if( ! is_file($this->_path) ) {
   $message = $this->_path . ' is not a file.';
   throw new ConvertException($message);
  }
  if( ! $this->_isWR() ) {
   $message = $this->_path . ' must can be read and write.';
   throw new ConvertException($message);
  }
  $file_real_path    = realpath($this->_path);
  $file_content_from = file_get_contents( $file_real_path );
  if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
   $file_content_to   = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
   file_put_contents( $file_real_path, $file_content_to );
  }
  return true;
 }
 /**
  * 转换目录
  * @throws ConvertException
  * @return boolean
  */
 private function _convertDirectory() {
  if( ! file_exists($this->_path) ) {
   $message = $this->_path . ' does not exist.';
   throw new ConvertException($message);
  }
  if( ! is_dir($this->_path) ) {
   $message = $this->_path . ' is not a directory.';
   throw new ConvertException($message);
  }
  if( ! $this->_isWR() ) {
   $message = $this->_path . ' must can be read and write.';
   throw new ConvertException($message);
  }
  $this->_scanDirFiles();
  if( empty($this->_files) ) {
   $message = $this->_path . ' is a empty directory.';
   throw new ConvertException($message);
  }
  foreach( $this->_files as $value ) {
   $file_content_from = file_get_contents( $value );
   if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
    $file_content_to   = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
    file_put_contents( $value, $file_content_to );
   }
  }
  return true;
 }
 /**
  * 判断文件或者目录是否可读写
  * @return boolean 可读写时返回true,否则返回false
  */
 private function _isWR() {
  if( is_readable($this->_path) && is_writable($this->_path) ) {
   return true;
  }
  return false;
 }
 /**
  * 遍历目录,找出所有文件,加上绝对路径
  * @return boolean
  */
 private function _scanDirFiles($dir = '') {
  $base_path = empty( $dir ) ? realpath($this->_path) . DIRECTORY_SEPARATOR : realpath($dir) . DIRECTORY_SEPARATOR;
  $files_tmp = empty( $dir ) ? scandir($this->_path) : scandir($dir);
  foreach( $files_tmp as $value ) {
   if( $value == '.' || $value == '..' || ( strpos($value, '.') === 0 ) ) {
    continue;
   }
   $value = $base_path . $value;
   if( is_dir($value) ) {
    if( $this->_recursion ) {
     $this->_scanDirFiles($value);
    }
   }
   elseif( is_file($value) ) {
    $this->_files[] = $value;
   }
  }
  return true;
 }
}

/**
 * 转换异常
 *
 */
class ConvertException extends Exception {
}
PHP 相关文章推荐
php面向对象全攻略 (九)访问类型
Sep 30 PHP
php实现模拟登陆方正教务系统抓取课表
May 19 PHP
PHP中使用substr()截取字符串出现中文乱码问题该怎么办
Oct 21 PHP
Yii2组件之多图上传插件FileInput的详细使用教程
Jun 20 PHP
thinkPHP+PHPExcel实现读取文件日期的方法(含时分秒)
Jul 07 PHP
php、java、android、ios通用的3des方法(推荐)
Sep 09 PHP
[原创]PHP实现生成vcf vcard文件功能类定义与使用方法详解【附demo源码下载】
Sep 02 PHP
PHP排序算法之冒泡排序(Bubble Sort)实现方法详解
Apr 20 PHP
PHP中抽象类,接口功能、定义方法示例
Feb 26 PHP
PHP implode()函数用法讲解
Mar 08 PHP
[原创]PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】
Jul 12 PHP
php使用自带dom扩展进行元素匹配的原理解析
May 29 PHP
php导出word文档与excel电子表格的简单示例代码
Mar 08 #PHP
php 创建以UNIX时间戳命名的文件夹(示例代码)
Mar 08 #PHP
PHP_Cooikes不同页面无法传递的解决方法
Mar 07 #PHP
php function用法如何递归及return和echo区别
Mar 07 #PHP
详解PHP中strlen和mb_strlen函数的区别
Mar 07 #PHP
解决Codeigniter不能上传rar和zip压缩包问题
Mar 07 #PHP
php 生成自动创建文件夹并上传文件的示例代码
Mar 07 #PHP
You might like
php强制运行广告的方法
2014/12/01 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
2016/09/21 PHP
php foreach如何跳出两层循环(详解)
2016/11/05 PHP
thinkPHP实现多字段模糊匹配查询的方法
2016/12/01 PHP
php中curl和soap方式请求服务超时问题的解决
2018/06/11 PHP
Jquery 获得服务器控件值的方法小结
2010/05/11 Javascript
jquery导航制件jquery鼠标经过变色效果示例
2013/12/05 Javascript
jquery实现鼠标悬浮停止轮播特效
2020/08/20 Javascript
详解vue渲染函数render的使用
2017/12/12 Javascript
vue.js简单配置axios的方法详解
2017/12/13 Javascript
JS 实现百度搜索功能
2018/02/01 Javascript
使用JS代码实现俄罗斯方块游戏
2018/08/03 Javascript
vue-cli3 从搭建到优化的详细步骤
2019/01/20 Javascript
Vue动态组件和异步组件原理详解
2019/05/06 Javascript
如何通过shell脚本自动生成vue文件详解
2019/09/10 Javascript
Openlayers实现点闪烁扩散效果
2020/09/24 Javascript
python3.3实现乘法表示例
2014/02/07 Python
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
python实现将excel文件转化成CSV格式
2018/03/22 Python
Python动态强类型解释型语言原理解析
2020/03/25 Python
python利用appium实现手机APP自动化的示例
2021/01/26 Python
自定义html标记替换html5新增元素
2008/10/17 HTML / CSS
美国求婚钻戒网站:Super Jeweler
2016/08/27 全球购物
戴尔加拿大官网:Dell加拿大
2016/09/17 全球购物
美国廉价机票预订网站:Cheapfaremart
2018/04/28 全球购物
Brasty波兰:香水、化妆品、手表网上商店
2019/04/15 全球购物
工商管理专业应届生求职信
2013/11/04 职场文书
应届实习生的自我评价范文
2014/01/05 职场文书
幼儿园教师辞职信
2014/01/18 职场文书
简单租房协议书范本
2014/08/20 职场文书
孩子教育的心得体会
2014/09/01 职场文书
民政局副局长民主生活会个人对照检查材料
2014/09/19 职场文书
上班迟到检讨书300字
2014/10/18 职场文书
合作协议书范本
2014/10/25 职场文书
Pandas||过滤缺失数据||pd.dropna()函数的用法说明
2021/05/14 Python
Windows中Redis安装配置流程并实现远程访问功能
2021/06/07 Redis