PHP implementation file Encoding batch conversion _php instance

Source: Internet
Author: User

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.

Copy Code code as follows:

<?php
/**
* Convert file Encoding
* Dependent extensions filesystem and mbstring
* @example
* <pre>
* 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>
*/
Class Convertencode {

/**
* The encoding to convert to
* @var String
*/
Private $_to_encoding;

/**
* Pre-conversion coding
* @var String
*/
Private $_from_encoding;

/**
* The directory or single file to be converted
* @var String
*/
Private $_path;

/**
* Whether is a directory, when given is the directory is set
* @var Boolean
*/
Private $_directory;

/**
* Recursive traversal, only valid for directory
* @var Boolean
*/
Private $_recursion;

/**
* Save all files to be converted, only when converting files in the directory
* @var Array
*/
Private $_files = Array ();

/**
* Constructor
*/
Public Function __construct () {
if (! function_exists (' mb_convert_encoding ')) {
throw new Convertexception (' mbstring extension be required ');
}
}

/**
* Set the directory or single file to be converted
* @param string $path directory or file
* @param boolean is a directory
* @param boolean whether the recursive directory
* @return Boolean
*/
Public Function SetPath ($path, $is _dir = False, $rec = False) {
$this->_path = $path;
$this->_directory = $is _dir;
$this->_recursion = $rec;
return true;
}

/**
* Set the encoding before conversion and the encoding to convert to
* @param string $encode the encoding before conversion
* @param string $encode converted to the encoding
* @return Boolean
*/
Public Function Setencode ($encode _from, $encode _to) {
$this->_from_encoding = $encode _from;
$this->_to_encoding = $encode _to;
return true;
}

/**
* Conversion encoding, depending on whether the directory is set up separately
* @return Boolean
*/
Public function convert () {
if ($this->_directory) {
return $this->_convertdirectory ();
}
return $this->_convertfile ();
}

/**
* 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-&GT;_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;

}

/**
* Conversion Directory
* @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-&GT;_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;
}

/**
* Determine if a file or directory is readable or writable
* @return Boolean can return True when read or write, otherwise return false
*/
Private Function _ISWR () {
if (is_readable ($this->_path) && is_writable ($this->_path)) {
return true;
}
return false;
}

/**
* Traverse directory, find all files, plus absolute path
* @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;
}
}

/**
* Conversion exception
*
*/
Class Convertexception extends Exception {

}

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.

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.