There are some problems that cannot be repeated, such as converting gbk to utf8, and then converting it to utf8. This will cause garbled characters. I tried to detect the encoding before conversion, and it seems that it failed. I tried a file specially. I checked whether it is gbk or UTF-8 and returned true. This does not understand.
Copy codeThe Code is as follows:
<? Php
/**
* Convert file encoding
* Dependent extension 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 {
/**
* 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 {
}