首頁 後端開發 php教程 批次或單一轉換文件編碼

批次或單一轉換文件編碼

Jul 25, 2016 am 08:48 AM

원본 gbk와 같은 파일 인코딩을 utf-8로 변환합니다. 단일 파일이나 전체 파일 디렉터리, 선택적으로 재귀 디렉터리를 변환할 수 있습니다.
예를 들어 gbk를 utf8로 변환한 다음 utf8로 변환하면 문자가 깨질 수 있습니다. 원래 변환 전에 인코딩을 감지하려고 시도했지만 실패한 것 같습니다. 구체적으로 파일을 시험해보고 그것이 gbk인지 utf-8인지 확인했는데 둘 다 true를 반환했습니다. 나는 이것을 이해하지 못한다.
  1. /**
  2. * 파일 인코딩 변환
  3. * 종속 확장 파일 시스템 및 mbstring
  4. * @example
  5. *
    </li>
    <li> * include_once 'ConvertEncode.php';</li>
    <li> * $convert = new ConvertEncode ();</li>
    <li> * try{</li>
    <li> * $convert->setPath('my', true, true);//Directory</li>
    <li> * //$convert->setPath('my.php ' );//단일 파일</li>
    <li> * $convert->setEncode('GBK', 'UTF-8');</li>
    <li> * $convert->convert();</li>
    <li> * }catch(ConvertException $ e) {</li>
    <li> * echo $e->getMessage();</li>
    <li> * }</li>
    <li> * 
  6. */
  7. class ConvertEncode {
  8. /**
  9. * 변환할 인코딩
  10. * @var string
  11. */
  12. private $_to_encoding;
  13. /**
  14. * 변환 전 인코딩
  15. * @var string
  16. */
  17. private $_from_encoding;
  18. /**
  19. * 변환할 디렉터리 또는 단일 파일
  20. * @var string
  21. */
  22. private $_path;
  23. /**
  24. * 디렉터리인지 여부는 해당 디렉터리가
  25. * @var boolean
  26. 일 때만 설정됩니다.*/
  27. private $_directory;
  28. /**
  29. * 재귀적으로 순회할지 여부, 디렉터리에만 유효
  30. * @var boolean
  31. */
  32. private $_recursion;
  33. /**
  34. * 변환할 모든 파일을 저장하고, 디렉터리에 있는 파일을 변환할 때만 사용합니다.
  35. * @var array
  36. */
  37. private $_files = array();
  38. /**
  39. * 생성자
  40. */
  41. public function __construct() {
  42. if( ! function_exists('mb_convert_encoding') ) {
  43. throw new ConvertException('mbstring 확장자가 필요함');
  44. }
  45. }
  46. /**
  47. * 변환할 디렉토리 또는 단일 파일을 설정
  48. * @param string $path 디렉토리 또는 파일
  49. * @param boolean 디렉토리인지 여부
  50. * @param boolean 재귀 디렉토리인지 여부
  51. * @return 부울
  52. */
  53. 공용 함수 setPath($path, $is_dir = false, $rec = false) {
  54. $this->_path = $path;
  55. $this->_directory = $is_dir;
  56. $this->_recursion = $rec;
  57. return true;
  58. }
  59. /**
  60. * 변환 전 인코딩과 변환할 인코딩을 설정
  61. * @param string $encode 변환 전 인코딩
  62. * @param string $encode 변환할 인코딩
  63. * @return boolean
  64. */
  65. 공개 함수 setEncode($encode_from, $encode_to) {
  66. $this->_from_encoding = $encode_from;
  67. $this->_to_encoding = $encode_to;
  68. return true;
  69. }
  70. /**
  71. * 인코딩 변환, 디렉토리 설정 여부에 따라 별도로 변환
  72. * @return boolean
  73. */
  74. 공용 함수 변환() {
  75. if($ this->_directory ) {
  76. return $this->_convertDirectory();
  77. }
  78. return $this->_convertFile();
  79. }
  80. /**
  81. * 파일 변환
  82. * @throws ConvertException
  83. * @return boolean
  84. */
  85. 비공개 함수 _convertFile() {
  86. if( ! file_exists($this->_path) ) {
  87. $message = $this->_path . '이 존재하지 않습니다.';
  88. throw new ConvertException($message);
  89. }
  90. if( !is_file($this->_path) ) {
  91. $message = $this-> _길 . '는 파일이 아닙니다.';
  92. throw new ConvertException($message);
  93. }
  94. if( ! $this->_isWR() ) {
  95. $message = $this-> _길 . '는 읽고 쓸 수 있어야 합니다.';
  96. throw new ConvertException($message);
  97. }
  98. $file_real_path = realpath($this->_path);
  99. $file_content_from = file_get_contents( $ file_real_path );
  100. if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
  101. $file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
  102. file_put_contents( $file_real_path, $file_content_to );
  103. }
  104. return true;
  105. }
  106. /**
  107. * 디렉토리 변환
  108. * @throws ConvertException
  109. * @return boolean
  110. */
  111. 비공개 함수 _convertDirectory() {
  112. if( !file_exists($this->_path) ) {
  113. $message = $this->_path . '이 존재하지 않습니다.';
  114. throw new ConvertException($message);
  115. }
  116. if( !is_dir($this->_path) ) {
  117. $message = $this-> _길 . '는 디렉토리가 아닙니다.';
  118. throw new ConvertException($message);
  119. }
  120. if( ! $this->_isWR() ) {
  121. $message = $this-> _길 . '는 읽고 쓸 수 있어야 합니다.';
  122. throw new ConvertException($message);
  123. }
  124. $this->_scanDirFiles();
  125. if(empty($this->_files ) ) {
  126. $message = $this->_path . '는 빈 디렉토리입니다.';
  127. throw new ConvertException($message);
  128. }
  129. foreach( $this->_files as $value ) {
  130. $file_content_from = file_get_contents( $value ) ;
  131. if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
  132. $file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
  133. file_put_contents ( $value, $file_content_to );
  134. }
  135. }
  136. true를 반환합니다.
  137. }
  138. /**
  139. * 判斷檔案或目錄是否可讀寫
  140. * @return boolean 可讀寫時回傳true,否則回傳false
  141. */
  142. 創函數_isWR() {
  143. if( is_read($this->_path) && is_writable($this->_path) ) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * 遍歷目錄,找出所有文件,加上絕對路徑
  150. * @return boolean
  151. */
  152. 私有函數_scanDirFiles($dir = '') {
  153. $base_path = 空( $dir ) ? realpath($this->_path) 。 DIRECTORY_SEPARATOR :真實路徑($dir) 。 scandir($this->_path) : scandir($dir);
  154. foreach( $files_tmp as $value ) {
  155. if( $value == '.' || $value == '..' | | ( strpos($value, '.') === 0 ) ) {
  156. 續;
  157. }
  158. $value = $base_path 。 {
  159. if( $this->_recursion ) {
  160. $this->_scanDirFiles($value);
  161. }
  162. }
  163. elseif( is_file($value) ) {
  164. $
  165. $ this->_files[] = $value;
  166. }
  167. }
  168. 回傳true;
  169. }
  170. }
  171. /**
  172. * 轉換異常
  173. *
  174. */
  175. class ConvertException 擴充了Exception {
  176. }
複製程式碼

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

解釋PHP中晚期靜態結合的概念。 解釋PHP中晚期靜態結合的概念。 Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

自定義/擴展框架:如何添加自定義功能。 自定義/擴展框架:如何添加自定義功能。 Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

See all articles