Conversion file encoding, such as the original is GBK, converted to Utf-8, can be transferred to a single file can also be converted to the entire directory of files, optional recursive directory. Some problems, can not repeat, such as GBK go to UTF8, and then have in turn into UTF8, this will be garbled, I originally tried to detect the code before the conversion, seemingly failed. I deliberately tried a file, and I checked whether it was GBK or utf-8, and all returned true. This is not understood.
- /**
- * Convert file Encoding
- * Dependent extensions 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 {
- /**
- * The encoding to be converted into
- * @var String
- */
- Private $_to_encoding;
- /**
- * Pre-conversion encoding
- * @var String
- */
- Private $_from_encoding;
- /**
- * The directory or single file to convert
- * @var String
- */
- Private $_path;
- /**
- * Whether it is a directory, when given is the directory is only 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 directory
- * @var Array
- */
- Private $_files = Array ();
- /**
- * Constructor function
- */
- 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 If Boolean is a directory
- * @param boolean is 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 conversion 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, based on whether the directory is set to convert separately
- * @return Boolean
- */
- Public function convert () {
- if ($this->_directory) {
- return $this->_convertdirectory ();
- }
- return $this->_convertfile ();
- }
- /**
- * Convert Files
- * @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 if a file or directory is readable or writable
- * Returns True @return Boolean is read-write, otherwise false
- */
- Private Function _ISWR () {
- if (is_readable ($this->_path) && is_writable ($this->_path)) {
- return true;
- }
- return false;
- }
- /**
- * Traverse directory to 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 {
- }
Copy Code |