Batch or single conversion file encoding

Source: Internet
Author: User
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.
  1. /**
  2. * Convert file Encoding
  3. * Dependent extensions filesystem and mbstring
  4. * @example
  5. *
  6. * include_once ' convertencode.php ';
  7. * $convert = new Convertencode ();
  8. * try{
  9. * $convert->setpath (' My ', true, true);//Directory
  10. * //$convert->setpath (' my.php ');//Single File
  11. * $convert->setencode (' GBK ', ' UTF-8 ');
  12. * $convert->convert ();
  13. *}catch (convertexception $e) {
  14. * echo $e->getmessage ();
  15. * }
  16. */
  17. Class Convertencode {
  18. /**
  19. * The encoding to be converted into
  20. * @var String
  21. */
  22. Private $_to_encoding;
  23. /**
  24. * Pre-conversion encoding
  25. * @var String
  26. */
  27. Private $_from_encoding;
  28. /**
  29. * The directory or single file to convert
  30. * @var String
  31. */
  32. Private $_path;
  33. /**
  34. * Whether it is a directory, when given is the directory is only set
  35. * @var Boolean
  36. */
  37. Private $_directory;
  38. /**
  39. * Recursive traversal, only valid for directory
  40. * @var Boolean
  41. */
  42. Private $_recursion;
  43. /**
  44. * Save all files to be converted, only when converting files in directory
  45. * @var Array
  46. */
  47. Private $_files = Array ();
  48. /**
  49. * Constructor function
  50. */
  51. Public Function __construct () {
  52. if (! function_exists (' mb_convert_encoding ')) {
  53. throw new Convertexception (' mbstring extension be required ');
  54. }
  55. }
  56. /**
  57. * Set the directory or single file to be converted
  58. * @param string $path directory or file
  59. * @param If Boolean is a directory
  60. * @param boolean is recursive directory
  61. * @return Boolean
  62. */
  63. Public Function SetPath ($path, $is _dir = False, $rec = False) {
  64. $this->_path = $path;
  65. $this->_directory = $is _dir;
  66. $this->_recursion = $rec;
  67. return true;
  68. }
  69. /**
  70. * Set the encoding before conversion and the encoding to convert to
  71. * @param string $encode the encoding before conversion
  72. * @param string $encode conversion to the encoding
  73. * @return Boolean
  74. */
  75. Public Function Setencode ($encode _from, $encode _to) {
  76. $this->_from_encoding = $encode _from;
  77. $this->_to_encoding = $encode _to;
  78. return true;
  79. }
  80. /**
  81. * Conversion encoding, based on whether the directory is set to convert separately
  82. * @return Boolean
  83. */
  84. Public function convert () {
  85. if ($this->_directory) {
  86. return $this->_convertdirectory ();
  87. }
  88. return $this->_convertfile ();
  89. }
  90. /**
  91. * Convert Files
  92. * @throws convertexception
  93. * @return Boolean
  94. */
  95. Private Function _convertfile () {
  96. if (! file_exists ($this->_path)) {
  97. $message = $this->_path. ' does not exist. ';
  98. throw new Convertexception ($message);
  99. }
  100. if (! Is_file ($this->_path)) {
  101. $message = $this->_path. ' is not a file. ';
  102. throw new Convertexception ($message);
  103. }
  104. if (! $this->_ISWR ()) {
  105. $message = $this->_path. ' must can be read and write. ';
  106. throw new Convertexception ($message);
  107. }
  108. $file _real_path = Realpath ($this->_path);
  109. $file _content_from = file_get_contents ($file _real_path);
  110. if (mb_check_encoding ($file _content_from, $this->_from_encoding)) {
  111. $file _content_to = mb_convert_encoding ($file _content_from, $this->_to_encoding, $this->_from_encoding);
  112. File_put_contents ($file _real_path, $file _content_to);
  113. }
  114. return true;
  115. }
  116. /**
  117. * Conversion Directory
  118. * @throws convertexception
  119. * @return Boolean
  120. */
  121. Private Function _convertdirectory () {
  122. if (! file_exists ($this->_path)) {
  123. $message = $this->_path. ' does not exist. ';
  124. throw new Convertexception ($message);
  125. }
  126. if (! Is_dir ($this->_path)) {
  127. $message = $this->_path. ' is not a directory. ';
  128. throw new Convertexception ($message);
  129. }
  130. if (! $this->_ISWR ()) {
  131. $message = $this->_path. ' must can be read and write. ';
  132. throw new Convertexception ($message);
  133. }
  134. $this->_scandirfiles ();
  135. if (Empty ($this->_files)) {
  136. $message = $this->_path. ' is a empty directory. ';
  137. throw new Convertexception ($message);
  138. }
  139. foreach ($this->_files as $value) {
  140. $file _content_from = file_get_contents ($value);
  141. if (mb_check_encoding ($file _content_from, $this->_from_encoding)) {
  142. $file _content_to = mb_convert_encoding ($file _content_from, $this->_to_encoding, $this->_from_encoding);
  143. File_put_contents ($value, $file _content_to);
  144. }
  145. }
  146. return true;
  147. }
  148. /**
  149. * Determine if a file or directory is readable or writable
  150. * Returns True @return Boolean is read-write, otherwise false
  151. */
  152. Private Function _ISWR () {
  153. if (is_readable ($this->_path) && is_writable ($this->_path)) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Traverse directory to find all files, plus absolute path
  160. * @return Boolean
  161. */
  162. Private Function _scandirfiles ($dir = ") {
  163. $base _path = Empty ($dir)? Realpath ($this->_path). Directory_separator:realpath ($dir). Directory_separator;
  164. $files _tmp = Empty ($dir)? Scandir ($this->_path): Scandir ($dir);
  165. foreach ($files _tmp as $value) {
  166. if ($value = = '. ' | | $value = = ' ... ' | | (Strpos ($value, '. ') = = = 0)) {
  167. Continue
  168. }
  169. $value = $base _path. $value;
  170. if (Is_dir ($value)) {
  171. if ($this->_recursion) {
  172. $this->_scandirfiles ($value);
  173. }
  174. }
  175. ElseIf (Is_file ($value)) {
  176. $this->_files[] = $value;
  177. }
  178. }
  179. return true;
  180. }
  181. }
  182. /**
  183. * Conversion exception
  184. *
  185. */
  186. Class Convertexception extends Exception {
  187. }
Copy Code
  • 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.