ホームページ  >  記事  >  バックエンド開発  >  ファイルエンコーディングをバッチまたは個別に変換します

ファイルエンコーディングをバッチまたは個別に変換します

WBOY
WBOYオリジナル
2016-07-25 08:48:46873ブラウズ
ファイル エンコーディングを変換します (元の gbk から utf-8 など)。単一のファイルまたはファイルのディレクトリ全体 (オプションで再帰ディレクトリ) を変換できます。
一部の問題は再現できません。たとえば、gbk を utf8 に変換すると、文字化けが発生します。最初は変換前にエンコードを検出しようとしましたが、失敗したようです。具体的にファイルを試してみて、それが gbk であるか utf-8 であるかを確認したところ、どちらも true を返しました。これはわかりません。
  1. /**
  2. * ファイルエンコーディングを変換します
  3. * 依存する拡張ファイルシステムとmbstring
  4. * @example
  5. *
  6. * include_once 'ConvertEncode.php';
  7. * $convert = new ConvertEncode();
  8. * try{
  9. * $convert - >setPath('my', true, true);//ディレクトリ
  10. * //$convert->setPath('my.php');//単一ファイル
  11. * $convert->setEncode('GBK ' , 'UTF-8');
  12. * $convert->convert();
  13. * }catch(ConvertException $e) {
  14. * echo $e->getMessage();
  15. * }
  16. * */
  17. class ConvertEncode {
  18. /**
  19. * 変換されるエンコーディング
  20. * @var string
  21. */
  22. private $_to_encoding;
  23. /**
  24. * 変換前のエンコーディング
  25. * @var 文字列
  26. */
  27. private $_from_encoding ;
  28. /**
  29. * 変換されるディレクトリまたは単一ファイル
  30. * @var 文字列
  31. */
  32. private $_path;
  33. /**
  34. * ディレクトリであるかどうかに関係なく、指定されたディレクトリが次の場合にのみ設定されます
  35. * @var boolean
  36. */
  37. private $_directory;
  38. /**
  39. * 再帰的に走査するかどうか、ディレクトリに対してのみ有効です
  40. * @var boolean
  41. */
  42. private $_recursion;
  43. /* *
  44. * 変換するすべてのファイルを保存します。ディレクトリ内のファイルを変換する場合にのみ使用されます
  45. * @var array
  46. */
  47. private $_files = array();
  48. /**
  49. *コンストラクター
  50. */
  51. public function __construct() {
  52. if( ! function_exists('mb_convert_encoding') ) {
  53. throw new ConvertException(' mbstring 拡張子が必要です');
  54. }
  55. }
  56. /**
  57. * 変換する必要があるディレクトリまたは単一ファイルを設定します
  58. * @param string $path ディレクトリまたはファイル
  59. * @param boolean ディレクトリかどうか
  60. * @param boolean 再帰ディレクトリかどうか
  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. * 変換前のエンコードと変換後のエンコードを設定します
  71. * @param string $encode 変換前のエンコード
  72. * @param string $encode 変換後のエンコード
  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. * エンコーディングを変換、ディレクトリ設定かどうかに応じて個別に変換します
  82. * @return boolean
  83. */
  84. public function Convert() {
  85. if($ this->_directory ) {
  86. return $this->_convertDirectory();
  87. }
  88. return $this->_convertFile();
  89. }
  90. /**
  91. * ファイルを変換します
  92. * @throws ConvertException
  93. * @return boolean
  94. */
  95. private function _convertFile() {
  96. if( ! file_exists($this->_path) ) {
  97. $message = $this->_path . ' は存在しません。';
  98. throw new ConvertException($message);
  99. }
  100. if( ! is_file($this->_path) ) {
  101. $message = $this->_path . ' はファイルではありません。';
  102. throw new ConvertException($message);
  103. }
  104. if( ! $this->_isWR() ) {
  105. $message = $this->_path . ' は読み取りと書き込みができる必要があります。';
  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. * ディレクトリを変換します
  118. * @throws ConvertException
  119. * @return boolean
  120. */
  121. プライベート関数 _convertDirectory() {
  122. if( ! file_exists($this->_path) ) {
  123. $message = $this->_path . ' は存在しません。';
  124. throw new ConvertException($message);
  125. }
  126. if( ! is_dir($this->_path) ) {
  127. $message = $this->_path . ' はディレクトリではありません。';
  128. throw new ConvertException($message);
  129. }
  130. if( ! $this->_isWR() ) {
  131. $message = $this->_path . ' 読み取りと書き込みができる必要があります。';
  132. throw new ConvertException($message);
  133. }
  134. $this->_scanDirFiles();
  135. if( empty($this->_files) ) {
  136. $message = $ this->_path 。 ' は空のディレクトリです。';
  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本当です;
  147. }
  148. /**
  149. * ファイルまたはディレクトリが読み取り可能および書き込み可能かどうかを判断します
  150. * @return boolean は、読み取りおよび書き込みができる場合は true を返し、そうでない場合は false を返します
  151. */
  152. プライベート関数 _isWR() {
  153. if( is_readable($this->_path) && is_writable($this->_path) ) {
  154. return true;
  155. }
  156. return false ;
  157. }
  158. /**
  159. * ディレクトリを走査し、すべてのファイルと絶対パスを検索します
  160. * @return boolean
  161. */
  162. プライベート関数 _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->gt;_recursion ) {
  172. $this->gt;_scanDirFiles($value);
  173. }
  174. }
  175. elseif( is_file($value) ) {
  176. $this->_files[] = $value;
  177. }
  178. }
  179. return true;
  180. }
  181. }
  182. /**
  183. *変換例外
  184. *
  185. */
  186. class ConvertException extends Exception {
  187. }
复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。