php实现文件编码批量转换

 更新时间:2014年03月10日 10:12:10   作者:  
转换文件编码,比如原来是gbk,转换成utf-8的,可以转单个文件也可以转换整个目录的文件,可选是否递归目录

有些问题,不能重复转,比如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公用函数列表[正则]

    php公用函数列表[正则]...
    2007-02-02
  • Thinkphp5.0框架使用模型Model的获取器、修改器、软删除数据操作示例

    Thinkphp5.0框架使用模型Model的获取器、修改器、软删除数据操作示例

    这篇文章主要介绍了Thinkphp5.0框架使用模型Model的获取器、修改器、软删除数据操作,结合实例形式分析了thinkPHP5.0模型Model获取器、修改器数据操作相关实现技巧与注意事项,需要的朋友可以参考下
    2019-10-10
  • PHP Laravel中的Trait使用方法

    PHP Laravel中的Trait使用方法

    在本篇文章里小编给各位分享了关于PHP Laravel中的Trait使用方法和相关知识点,有需要的朋友们学习下。
    2019-01-01
  • php实现单笔转账到支付宝功能

    php实现单笔转账到支付宝功能

    这篇文章主要为大家详细介绍了php实现单笔转账到支付宝功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • php读取3389的脚本

    php读取3389的脚本

    通过php获取系统3389端口信息的脚本,这里只分享实现方法,不建议非法用途
    2014-05-05
  • PHP网页游戏学习之Xnova(ogame)源码解读(六)

    PHP网页游戏学习之Xnova(ogame)源码解读(六)

    这篇文章主要介绍了PHP网页游戏Xnova(ogame)源码解读的公共代码,需要的朋友可以参考下
    2014-06-06
  • gd库图片下载类实现下载网页所有图片的php代码

    gd库图片下载类实现下载网页所有图片的php代码

    在前期的php教程就讲了php gd库可以实现远程图片的下载,但是那只是下载了一张图片,原理是一样的,要想下载一个网页的所有图片只要使用正则表达式进行判断,找出所有的图片url就可以进行循环下载了,我特地参照网络资源编写了gd库图片下载类!
    2012-08-08
  • 大家须知简单的php性能优化注意点

    大家须知简单的php性能优化注意点

    通过本文给大家介绍在什么情况下可能遇到性能问题,php性能问题的解决方向及优化点,对php性能优化注意点相关知识感兴趣的朋友一起学习吧
    2016-01-01
  • Yii数据模型中rules类验证器用法分析

    Yii数据模型中rules类验证器用法分析

    这篇文章主要介绍了Yii数据模型中rules类验证器用法,结合实例形式分析了rules类验证器的简单使用方法,代码注释中包含了相关方法的使用说明,需要的朋友可以参考下
    2016-07-07
  • Laravel如何友好的修改.env配置文件详解

    Laravel如何友好的修改.env配置文件详解

    使用laravel框架开发PHP程序,配置框架的.env文件是至关重要的,这个文件上需要配置数据库、数据库用户以及缓存等。下面这篇文章主要给大家介绍了Laravel如何友好的修改.env配置文件的相关资料,需要的朋友可以参考下。
    2017-06-06

最新评论