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

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP的目的:構建動態網站PHP的目的:構建動態網站Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP:處理數據庫和服務器端邏輯PHP:處理數據庫和服務器端邏輯Apr 15, 2025 am 12:15 AM

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

您如何防止PHP中的SQL注入? (準備的陳述,PDO)您如何防止PHP中的SQL注入? (準備的陳述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python:代碼示例和比較PHP和Python:代碼示例和比較Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP行動:現實世界中的示例和應用程序PHP行動:現實世界中的示例和應用程序Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:輕鬆創建交互式Web內容PHP:輕鬆創建交互式Web內容Apr 14, 2025 am 12:15 AM

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python:比較兩種流行的編程語言PHP和Python:比較兩種流行的編程語言Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP的持久相關性:它還活著嗎?PHP的持久相關性:它還活著嗎?Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

See all articles

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

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版

SublimeText3 Mac版

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

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能