一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php實(shí)現(xiàn)文件編碼批量轉(zhuǎn)換

有些問題,不能重復(fù)轉(zhuǎn),比如gbk轉(zhuǎn)到utf8,然后有在轉(zhuǎn)成utf8,這樣會亂碼,我本來試圖在轉(zhuǎn)換之前去檢測編碼的,貌似失敗了。我特意試了一個文件,我檢測它是是否是gbk或者是utf-8,都返回true。這就不懂了。

復(fù)制代碼 代碼如下:
<?php
/**
 * 轉(zhuǎn)換文件編碼
 * 依賴的擴(kuò)展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 {

 /**
  * 要轉(zhuǎn)換成的編碼
  * @var string
  */
 private $_to_encoding;

 /**
  * 轉(zhuǎn)換前的編碼
  * @var string
  */
 private $_from_encoding;

 /**
  * 要轉(zhuǎn)換的的目錄或者單文件
  * @var string
  */
 private $_path;

 /**
  * 是否是一個目錄,當(dāng)給出的是目錄是才設(shè)置
  * @var boolean
  */
 private $_directory;

 /**
  * 是否遞歸遍歷,僅對目錄有效
  * @var boolean
  */
 private $_recursion;

 /**
  * 保存所有待轉(zhuǎn)換的文件,僅當(dāng)轉(zhuǎn)換目錄里面的文件時才用
  * @var array
  */
 private $_files = array();

 /**
  * 構(gòu)造函數(shù)
  */
 public function __construct() {
  if( ! function_exists('mb_convert_encoding') ) {
   throw new ConvertException('mbstring extension be required');
  }
 }

 /**
  * 設(shè)置需要轉(zhuǎn)換的目錄或者單文件
  * @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;
 }

 /**
  * 設(shè)置轉(zhuǎn)換前的編碼和要轉(zhuǎn)換到的編碼
  * @param string $encode 轉(zhuǎn)換前的編碼
  * @param string $encode 轉(zhuǎn)換到的編碼
  * @return boolean
  */
 public function setEncode($encode_from, $encode_to) {
  $this->_from_encoding = $encode_from;
  $this->_to_encoding   = $encode_to;
  return true;
 }

 /**
  * 轉(zhuǎn)換編碼,根據(jù)是否是目錄的設(shè)置分別轉(zhuǎn)換
  * @return boolean
  */
 public function convert() {
  if($this->_directory ) {
   return $this->_convertDirectory();
  }
  return $this->_convertFile();
 }

 /**
  * 轉(zhuǎn)換文件
  * @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;

 }

 /**
  * 轉(zhuǎn)換目錄
  * @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;
 }
}

/**
 * 轉(zhuǎn)換異常
 *
 */
class ConvertException extends Exception {

}

php技術(shù)php實(shí)現(xiàn)文件編碼批量轉(zhuǎn)換,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 一级美女毛片 | 国产网站精品 | 91蝌蚪视频在线观看 | 日韩久久中文字幕 | 五月婷婷丁香在线 | 中文字幕久久亚洲一区 | 成人免费观看在线网址 | 久久精品国产亚洲精品 | 精品国产免费第一区二区 | 国产成人精品一区二区仙踪林 | 国产91精品久久久久久 | 天天色综合天天 | 伊人狼人久久 | 久久久久综合网 | 色噜噜视频| 国产综合色在线视频区 | 免费看的黄网站 | 精品久久久久久午夜 | 激情五月婷婷基地 | 国产高清一区二区三区四区 | 久久久久avav久久久 | 久久亚洲精品永久网站 | 国产成人永久免费视频 | 六月婷婷七月丁香 | 亚洲色图偷 | 色哟哟小说 | 韩国资源视频一区二区三区 | 亚洲视频免费一区 | 欧美人与物videos另 | 国产高清视频在线 | 女人笫一次一级毛片 | 国产91久久最新观看地址 | 亚洲精品高清国产一久久 | 黑人美国一级一级爰片 | 日本午夜vr影院新入口 | 中文字幕精品一区二区日本 | 中文字幕精品一区影音先锋 | 成人午夜视频免费观看 | 国产成人高清 | 伊人免费 | 欧美一级日韩一级亚洲一级 |