PHP implementation file Encoding batch Conversion

Source: Internet
Author: User
Tags foreach

  Conversion file encoding, such as GBK, converted to Utf-8, can be transferred to a single file can also convert the entire directory of files, optional recursive directory

Some problems, can not repeat, such as GBK to UTF8, and then have in turn into UTF8, this will be garbled, I was trying to change before the conversion to detect the code, seemingly failed. I purposely tried a file, and I checked whether it was GBK or Utf-8, and returned true. This is not understood.     Code is as follows: <?php/**  * conversion file Encoding  * dependent extensions filesystem and mbstring  * @example  * <pre> &nb sp;* include_once ' convertencode.php ';  * $convert = new Convertencode ();  * try{ *   $convert->setpath (' My ', true, true);//directory  *    //$convert->setpath (' My.php ');//Single file  *   $convert->setencode (' GBK ', ' UTF-8 ');  *   $convert->convert ();  *}catch (convertexception $e) { *   echo $e->getmessage ();  *}  * </pre>  */CLA SS Convertencode {   /**   * To convert to code   * @var string  / private $_to_encoding;   &N bsp;/**   * Pre-conversion Coding   * @var string   */ private $_from_encoding;    /**   * directory to convert or single file   * @var string   */ private $_path;    /**   * is a directory, when given is the directory isBefore setting   * @var boolean  / private $_directory;    /**   * Whether recursive traversal, only the directory valid   * @var boolean  / private $_recursion;    /**   * Save all files to be converted, only use   * @var array  / private $_files = Array () When converting files in the directory.    /**   * constructor  / public function __construct () {  if (! function_exists (' Mb_convert_en Coding ') {   throw new convertexception (' mbstring extension be required ');  }  }    /**   * Set the directory to be converted or a single file   * @param string $path directory or file   * @param boolean is the directory   * @param boolean whether recursive directory & nbsp * @return Boolean   *  public function SetPath ($path, $is _dir = False, $rec = False) {  $this->_path = $path;   $this->_directory = $is _dir;   $this->_recursion = $rec;   return true; &NBSP}    /**   * Set the encoding before conversion and the encoding to be converted   * @param string $encode before conversion code   * @param string $encode Conversion to the encoding   * @return Boolean  / public function Setencode ($encode _from, $encode _to) {  $this->_from_enc oding = $encode _from;   $this->_to_encoding   = $encode _to;   return true; &NBSP}    /**   * Conversion encoding, depending on whether the directory is set to convert   * @return Boolean  / public function convert () {   if ($this->_directory) {   return $this->_convertdirectory ();  }   return $this->_c Onvertfile (); &NBSP}    /**   * Convert file   * @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;   &NBSP}    /**   * Convert directory   * @throws convertexception   * @return Boolean  / p rivate function _convertdirectory () {  if (! file_exists ($this->_path)) {    $message = $this->_pat H. ' does not exist. '    throw New Convertexception ($message);  }   if (! Is_dir ($this->_path)) {    $message = $this->_path. ' is not a directory. ' &nbsP  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); &nb Sp  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);   &NBSP}  }   return true; &NBSP}    /**   * To determine whether a file or directory can read/write   * @return Boolean can return True when read or write, otherwise return false   * */ private func tion _ISWR () {&nbsP if (is_readable ($this->_path) && is_writable ($this->_path)) {   return true;  }   RET Urn false; &NBSP}    /**   * Traverse directory, find all files, plus absolute path   * @return boolean   */ private function _scandirfil ES ($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   &nbsp}     $value = $base _path. $value;    if (Is_dir ($value)) {    if ($this->_recursion) {      $this->_scandirfiles ($value);    }   &NBSP}    elseif (Is_file ($value)) {    $this->_files[] = $value;   &NBSP}  }   return true; &NBSP}}  /** &NBsp;* Conversion Exception  *  */class Convertexception extends Exception { }  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.