最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年03月10日 10:12:10   作者:  
轉(zhuǎn)換文件編碼,比如原來是gbk,轉(zhuǎn)換成utf-8的,可以轉(zhuǎn)單個(gè)文件也可以轉(zhuǎn)換整個(gè)目錄的文件,可選是否遞歸目錄

有些問題,不能重復(fù)轉(zhuǎn),比如gbk轉(zhuǎn)到utf8,然后有在轉(zhuǎn)成utf8,這樣會(huì)亂碼,我本來試圖在轉(zhuǎn)換之前去檢測編碼的,貌似失敗了。我特意試了一個(gè)文件,我檢測它是是否是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;

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

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

 /**
  * 保存所有待轉(zhuǎn)換的文件,僅當(dāng)轉(zhuǎn)換目錄里面的文件時(shí)才用
  * @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 可讀寫時(shí)返回true,否則返回false
  */
 private function _isWR() {
  if( is_readable($this->_path) && is_writable($this->_path) ) {
   return true;
  }
  return false;
 }

 /**
  * 遍歷目錄,找出所有文件,加上絕對(duì)路徑
  * @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 {

}

相關(guān)文章

  • php公用函數(shù)列表[正則]

    php公用函數(shù)列表[正則]

    php公用函數(shù)列表[正則]...
    2007-02-02
  • Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例

    Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例

    這篇文章主要介紹了Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了thinkPHP5.0模型Model獲取器、修改器數(shù)據(jù)操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-10-10
  • PHP Laravel中的Trait使用方法

    PHP Laravel中的Trait使用方法

    在本篇文章里小編給各位分享了關(guān)于PHP Laravel中的Trait使用方法和相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。
    2019-01-01
  • php實(shí)現(xiàn)單筆轉(zhuǎn)賬到支付寶功能

    php實(shí)現(xiàn)單筆轉(zhuǎn)賬到支付寶功能

    這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)單筆轉(zhuǎn)賬到支付寶功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • php讀取3389的腳本

    php讀取3389的腳本

    通過php獲取系統(tǒng)3389端口信息的腳本,這里只分享實(shí)現(xiàn)方法,不建議非法用途
    2014-05-05
  • PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)

    PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)

    這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下
    2014-06-06
  • gd庫圖片下載類實(shí)現(xiàn)下載網(wǎng)頁所有圖片的php代碼

    gd庫圖片下載類實(shí)現(xiàn)下載網(wǎng)頁所有圖片的php代碼

    在前期的php教程就講了php gd庫可以實(shí)現(xiàn)遠(yuǎn)程圖片的下載,但是那只是下載了一張圖片,原理是一樣的,要想下載一個(gè)網(wǎng)頁的所有圖片只要使用正則表達(dá)式進(jìn)行判斷,找出所有的圖片url就可以進(jìn)行循環(huán)下載了,我特地參照網(wǎng)絡(luò)資源編寫了gd庫圖片下載類!
    2012-08-08
  • 大家須知簡單的php性能優(yōu)化注意點(diǎn)

    大家須知簡單的php性能優(yōu)化注意點(diǎn)

    通過本文給大家介紹在什么情況下可能遇到性能問題,php性能問題的解決方向及優(yōu)化點(diǎn),對(duì)php性能優(yōu)化注意點(diǎn)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法分析

    Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法分析

    這篇文章主要介紹了Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法,結(jié)合實(shí)例形式分析了rules類驗(yàn)證器的簡單使用方法,代碼注釋中包含了相關(guān)方法的使用說明,需要的朋友可以參考下
    2016-07-07
  • Laravel如何友好的修改.env配置文件詳解

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

    使用laravel框架開發(fā)PHP程序,配置框架的.env文件是至關(guān)重要的,這個(gè)文件上需要配置數(shù)據(jù)庫、數(shù)據(jù)庫用戶以及緩存等。下面這篇文章主要給大家介紹了Laravel如何友好的修改.env配置文件的相關(guān)資料,需要的朋友可以參考下。
    2017-06-06

最新評(píng)論

泰来县| 甘谷县| 宜黄县| 平罗县| 高邑县| 秦皇岛市| 讷河市| 东台市| 东光县| 新乐市| 岗巴县| 庆城县| 平舆县| 泰顺县| 塔河县| 西盟| 米易县| 汨罗市| 海城市| 乌兰浩特市| 曲周县| 荥经县| 宁城县| 察雅县| 治县。| 股票| 安阳市| 裕民县| 古田县| 渭源县| 石屏县| 禹城市| 皋兰县| 淮安市| 花垣县| 吐鲁番市| 遂昌县| 怀集县| 新源县| 靖江市| 东安县|