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 相关文章推荐
一个高ai的分页函数和一个url函数
Oct 09 PHP
一个没有MYSQL数据库支持的简易留言本的编写
Oct 09 PHP
PHP警告Cannot use a scalar value as an array的解决方法
Jan 11 PHP
解析curl提交GET,POST,Cookie的简单方法
Jun 29 PHP
PHP邮件发送类PHPMailer用法实例详解
Sep 22 PHP
PHP简单获取视频预览图的方法
Mar 12 PHP
CI框架整合smarty步骤详解
May 19 PHP
ThinkPHP中Common/common.php文件常用函数功能分析
May 20 PHP
PHP7下协程的实现方法详解
Dec 17 PHP
PHP简单实现正则匹配省市区的方法
Apr 13 PHP
PHP join()函数用法与实例讲解
Mar 11 PHP
tp5.1 框架路由操作-URL生成实例分析
May 26 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 和 COM
2006/10/09 PHP
一个PHP的QRcode类与大家分享
2011/11/13 PHP
php获取访问者IP地址汇总
2015/04/24 PHP
PHP7基于curl实现的上传图片功能
2018/05/11 PHP
yii2.0框架实现上传excel文件后导入到数据库的方法示例
2020/04/13 PHP
快速保存网页中所有图片的方法
2006/06/23 Javascript
JavaScript 拾碎[三] 使用className属性
2010/10/16 Javascript
Mac OS X 系统下安装和部署Egret引擎开发环境
2014/09/03 Javascript
JS小游戏之极速快跑源码详解
2014/09/25 Javascript
SyntaxHighlighter 3.0.83使用笔记
2015/01/26 Javascript
JavaScript显示当前文档最后修改日期的方法
2015/03/19 Javascript
JS原型链怎么理解
2016/06/27 Javascript
JavaScript循环_动力节点Java学院整理
2017/06/28 Javascript
layui添加动态菜单与选项卡
2019/07/26 Javascript
Python中使用hashlib模块处理算法的教程
2015/04/28 Python
Python中的条件判断语句基础学习教程
2016/02/07 Python
Python 中pandas.read_excel详细介绍
2017/06/23 Python
python获取当前目录路径和上级路径的实例
2018/04/26 Python
用Python徒手撸一个股票回测框架搭建【推荐】
2019/08/05 Python
python爬取本站电子书信息并入库的实现代码
2020/01/20 Python
python3.6中anaconda安装sklearn踩坑实录
2020/07/28 Python
python输入中文的实例方法
2020/09/14 Python
利用python绘制中国地图(含省界、河流等)
2020/09/21 Python
Python collections.deque双边队列原理详解
2020/10/05 Python
一款利用纯css3实现的360度翻转按钮的实例教程
2014/11/05 HTML / CSS
CSS3中引入多种自定义字体font-face
2020/06/12 HTML / CSS
Timberland俄罗斯官方网上商店:全球领先的户外品牌
2020/03/15 全球购物
网络工程师面试(三木通信技术有限公司)
2013/06/05 面试题
大专生毕业的自我评价
2014/02/06 职场文书
平安校园建设方案
2014/05/02 职场文书
标准版离职证明书
2014/09/12 职场文书
2014个人年终工作总结范文
2014/12/15 职场文书
撤诉状格式范本
2015/05/19 职场文书
十月围城观后感
2015/06/08 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
教你nginx跳转配置的四种方式
2022/07/07 Servers