php實現檔案編碼批量轉換

來源:互聯網
上載者:User

 轉換檔編碼,比如原來是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 { } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.