Php implements batch conversion of file encoding

Source: Internet
Author: User
Convert the file encoding. for example, if it was originally gbk and converted to UTF-8, you can convert a single file or the entire directory file. you can choose whether to recursive the directory, for example, if gbk is converted to utf8 and then converted to utf8, garbled characters will occur. I tried to detect the encoding before conversion, and it seems to have failed. I tried a file specially. I checked whether it is gbk or UTF-8 and returned true. This does not understand.

The code is as follows:
/**
* Convert file encoding
* Dependent extension filesystem and mbstring
* @ Example
*


* 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 ();
*}
*

*/
Class ConvertEncode {

/**
* Encoding to be converted
* @ Var string
*/
Private $ _ to_encoding;

/**
* Encoding before conversion
* @ Var string
*/
Private $ _ from_encoding;

/**
* Directory or single file to be converted
* @ Var string
*/
Private $ _ path;

/**
* Whether it is a directory. it is set only when the given directory is
* @ Var boolean
*/
Private $ _ directory;

/**
* Whether to recursively traverse the directory. this parameter is only valid for directories.
* @ Var boolean
*/
Private $ _ recursion;

/**
* All files to be converted are saved and used only when the files in the conversion directory are used.
* @ 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 whether it is a directory
* @ Param boolean 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 be converted
* @ Param string $ encoding before encode conversion
* @ Param string $ encoding converted to encode
* @ Return boolean
*/
Public function setEncode ($ encode_from, $ encode_to ){
$ This-> _ from_encoding = $ encode_from;
$ This-> _ to_encoding = $ encode_to;
Return true;
}

/**
* Conversion encoding, which is based on whether the directory is set or not
* @ Return boolean
*/
Public function convert (){
If ($ this-> _ directory ){
Return $ this-> _ convertDirectory ();
}
Return $ this-> _ convertFile ();
}

/**
* Conversion 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;

}

/**
* 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-> _ 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 whether a file or directory can be read or written
* @ Return boolean true is returned when read/write is enabled; otherwise, false is returned.
*/
Private function _ isWR (){
If (is_readable ($ this-> _ path) & is_writable ($ this-> _ path )){
Return true;
}
Return false;
}

/**
* Traverse the directory to find all the files and add the 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 {

}

Related Article

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.