天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

php實現文件編碼批量轉換

有些問題,不能重復轉,比如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實現文件編碼批量轉換,轉載需保留來源!

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

主站蜘蛛池模板: 欧美牲交A欧美牲交VDO | 欧美性色生活片天天看99顶级 | 国产h视频在线观看免费 | 久久精品熟一区二区三区 | 日本2021免费一二三四区 | 国产婷婷色一区二区三区在线 | 亚洲男女羞羞无遮挡久久丫 | 成人免费视频在线播放 | 一个人免费观看HD完整版 | 无人区在线日本高清免费 | 亚洲精品中文字幕无码A片蜜桃 | 亚洲在线v观看免费国 | 成人欧美一区二区三区白人 | WWW久久只有这里有精品 | 777米奇色狠狠俺去啦 | 女人高潮久久久叫人喷水 | 欧美影院在线观看完整版 mp4 | 久久99精品国产免费观看 | 暖暖 免费 高清 日本 在线 | 国产成人a v在线影院 | 99久久999久久久综合精品涩 | 国产成人永久免费视频 | 麻豆一区二区三区蜜桃免费 | 18禁黄无遮挡禁游戏在线下载 | 国产 亚洲 日韩 欧美 在线观看 | 乳色吐息在线观看全集免费观看 | 国产午夜精品一区二区三区 | 花季v3.0.2黄在线观看 | 欧美日韩视频一区二区三区 | 女警被黑人20厘米强交 | 亚洲狠狠97婷婷综合久久久久 | 伊人国产在线视频 | 蜜芽丅v新网站在线观看 | 乐乐亚洲精品综合影院 | 男人和女人一起愁愁愁很痛 | 免费鲁丝片一级在线观看 | 秋霞电影网午夜鲁丝片 | 18禁黄无遮挡禁游戏在线下载 | 中文字幕在线观看亚洲 | 免费撕开胸罩吮胸视频 | 俄罗斯人与动ZOZ0 |