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 相关文章推荐
zend api扩展的php对象的autoload工具
Apr 18 PHP
php设计模式 Proxy (代理模式)
Jun 26 PHP
PHP中header和session_start前不能有输出原因分析
Jan 11 PHP
PHP 5.3新增魔术方法__invoke概述
Jul 23 PHP
Yii 快速,安全,专业的PHP框架
Sep 03 PHP
PHP导入导出Excel代码
Jul 07 PHP
php把数组值转换成键的方法
Jul 13 PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 PHP
PHP中调用C/C++制作的动态链接库的教程
Mar 10 PHP
浅析Yii2集成富文本编辑器redactor实例教程
Apr 25 PHP
PHP随机数 C扩展随机数
May 04 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
May 02 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
ThinkPHP的I方法使用详解
2014/06/18 PHP
smarty内置函数section的用法
2015/01/22 PHP
laravel框架中路由设置,路由参数和路由命名实例分析
2019/11/23 PHP
CL vs ForZe BO5 第四场 2.13
2021/03/10 DOTA
拖动一个HTML元素
2006/12/22 Javascript
jQuery-serialize()输出序列化form表单值的方法
2012/12/26 Javascript
ajax请求get与post的区别总结
2013/11/04 Javascript
JS中window.open全屏命令解析及使用示例
2013/12/11 Javascript
原生js编写设为首页兼容ie、火狐和谷歌
2014/06/05 Javascript
浅谈javascript 函数属性和方法
2015/01/21 Javascript
js结合正则实现国内手机号段校验
2015/06/19 Javascript
基于Angularjs实现分页功能
2016/05/30 Javascript
javaScript知识点总结(必看篇)
2016/06/10 Javascript
angularjs的select使用及默认选中设置
2017/04/08 Javascript
移动前端图片压缩上传的实例
2017/12/06 Javascript
使用 Node.js 开发资讯爬虫流程
2018/01/07 Javascript
详解微信小程序实现仿微信聊天界面(各种细节处理)
2019/02/17 Javascript
Django中模型Model添加JSON类型字段的方法
2015/06/17 Python
django实现登录时候输入密码错误5次锁定用户十分钟
2017/11/05 Python
python3实现跳一跳点击跳跃
2018/01/08 Python
matplotlib实现区域颜色填充
2019/03/18 Python
Django Admin中增加导出CSV功能过程解析
2019/09/04 Python
Pyqt5 关于流式布局和滚动条的综合使用示例代码
2020/03/24 Python
Python如何根据时间序列数据作图
2020/05/12 Python
python获取时间戳的实现示例(10位和13位)
2020/09/23 Python
python如何编写类似nmap的扫描工具
2020/11/06 Python
环保专业大学生职业规划设计
2014/01/10 职场文书
小学教师自我鉴定范文
2014/03/20 职场文书
硕士生找工作求职信
2014/07/05 职场文书
农村环境卫生倡议书
2015/04/29 职场文书
医者仁心观后感
2015/06/17 职场文书
班级管理经验交流材料
2015/11/02 职场文书
家庭教育培训学习心得体会
2016/01/14 职场文书
2016大学优秀学生干部事迹材料
2016/03/01 职场文书
Python中Permission denied的解决方案
2021/04/02 Python
Golang中interface{}转为数组的操作
2021/04/30 Golang